无法将类型 'int?' 隐式转换为 'System.Collections.Generic.List<Model>
Cannot implicitly convert type 'int?' to 'System.Collections.Generic.List<Model>
我是 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 表达式,它会以某种方式给出一个列表吗?
数据库table代码:
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 生成的 class。
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; }
}
根据您的代码,MasterCategory 是一个 List
,您需要从数据库中通过 id 获取它。 List
可以使用 constructor with IEnumerable
作为参数来创建:
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 扩展方法都可用:
using System.Linq;
您必须像这样加入表格:
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()
};
阅读后有答案this post .
这一点有效。
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。
我是 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 表达式,它会以某种方式给出一个列表吗?
数据库table代码:
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 生成的 class。
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; }
}
根据您的代码,MasterCategory 是一个 List
,您需要从数据库中通过 id 获取它。 List
可以使用 constructor with IEnumerable
作为参数来创建:
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 扩展方法都可用:
using System.Linq;
您必须像这样加入表格:
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()
};
阅读后有答案this post .
这一点有效。
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。