我是MVC和LINQ的新手。
我正在尝试使用LINQ为视图提供模型。 该模型由另一个模型列表组成。
这是第一个型号。
public class MasterCategoryModel
{
[Key]
public int CategoryId { get; set; }
[Required(ErrorMessage = "Category name is mandatory")]
[StringLength(255, ErrorMessage = "Category name cannot exceed 255 characters")]
[Display(Name = "Category name ")]
public string Name {get;set;}
[StringLength(1000, ErrorMessage = "Category description cannot exceed 1000 characters")]
[Display(Name = "Category description")]
public string Description{get;set;}
}
这是更有问题的模型:
public class CategoryModel
{
[Key]
public int CategoryId { get; set; }
[Required]
[StringLength(255, ErrorMessage = "Category name cannot exceed 255 characters")]
[Display(Name = "Category nam")]
public string Name { get; set; }
[StringLength(1000, ErrorMessage = "Category description cannot exceed 1000 characters")]
[Display(Name = "Category description")]
public string Description { get; set; }
[Required]
public List<MasterCategoryModel> MasterCategory { get; set; }
}
在我的控制器中,我目前有以下内容:
[Authorize(Users = "Administrator")]
[HttpGet]
public PartialViewResult _SubCategory()
{
using (var db = new TSCEntities())
{
var query = from p in db.Categories
where p.ParentCategoryId != null
select new CategoryModel()
{
CategoryId = p.CategoryId,
Name = p.Name,
Description = p.Description,
MasterCategory = p.ParentCategoryId
};
return PartialView(query.ToList());
}
}
MasterCategory = p.ParentCategoryId
失败。
任何人都可以帮助LINQ表达式,它会以某种方式给出List吗?
数据库表代码:
CREATE TABLE [product].[Categories](
[CategoryId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](255) NOT NULL,
[Description] [nvarchar](1000) NULL,
[ParentCategoryId] [int] NULL,
PRIMARY KEY CLUSTERED
(
[CategoryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [product].[Categories] WITH CHECK ADD CONSTRAINT [FK_Category_ParentCategory] FOREIGN KEY([ParentCategoryId])
REFERENCES [product].[Categories] ([CategoryId])
GO
ALTER TABLE [product].[Categories] CHECK CONSTRAINT [FK_Category_ParentCategory]
GO
这是由EF生成的类。
public partial class Category
{
public Category()
{
this.Categories1 = new HashSet<Category>();
}
public int CategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Nullable<int> ParentCategoryId { get; set; }
public virtual ICollection<Category> Categories1 { get; set; }
public virtual Category Category1 { get; set; }
}
答案 0 :(得分:0)
根据您的代码,MasterCategory是List
,您需要从数据库中通过id获取它。可以使用constructor with IEnumerable
作为参数创建List
:
var query = from p in db.Categories
where p.ParentCategoryId != null
select new CategoryModel()
{
CategoryId = p.CategoryId,
Name = p.Name,
Description = p.Description,
MasterCategory = new List<MasterCategoryModel>((from c in db.Categories where c.CategoryId == p.ParentCategoryId select c).AsEnumerable())
};
<强>更新强>
我认为由于缺少using
指令,并非所有Linq Extension方法都可用:
using System.Linq;
答案 1 :(得分:0)
你必须加入这样的表:
var query = from p in db.Categories
join c in db.Categories on p.ParentCategoryId equals c.CategoryId
where p.ParentCategoryId != null
select new CategoryModel()
{
CategoryId = p.CategoryId,
Name = p.Name,
Description = p.Description,
MasterCategory = c.ToList().Select(x=>
new MasterCategoryModel
{
CategoryId= x.CategoryId,
Name = x.Name,
Description = x.Description
}).ToList()
};
答案 2 :(得分:0)
在阅读this帖后回答。
这一点有效。
var query = from p in db.Categories
where p.ParentCategoryId != null
select new CategoryModel()
{
CategoryId = p.CategoryId,
Name = p.Name,
Description = p.Description,
MasterCategory = new List<MasterCategoryModel>((from c in db.Categories where c.CategoryId == p.ParentCategoryId select new MasterCategoryModel { CategoryId = c.CategoryId, Name = c.Name, Description = c.Description}).AsEnumerable().ToList())
};
非常感谢大家,特别是@Ehsan Sajjad和@VMAtm。