当我尝试连接三个表ProductPhoto
, Product
和ProductProductPhoto
时,我遇到了这个问题。我试了很多案例,但都没有成功。有人能帮我脱下来吗?谢谢你的回复!
private IEnumerable<SubCateViewModel> GetSubProduct()
{
var context = new AdvenDBEntities();
var subcate = (from a in context.ProductPhotoes
join b in context.ProductProductPhotoes on a.ProductPhotoID equals b.ProductPhotoID
join c in context.Products on b.ProductID equals c.ProductID
join d in context.ProductSubcategories on c.ProductSubcategoryID equals d.ProductSubcategoryID
select new
{
a.ProductPhotoID,
a.ThumbnailPhotoFileName,
c.ProductID,
c.Name,
d.NameofBike,
d.isSelected,
}).AsQueryable();
return subcate;
}
试试下面的代码…
private IEnumerable<SubCateViewModel> GetSubProduct()
{
var context = new AdvenDBEntities();
var subcate = (from a in context.ProductPhotoes
join b in context.ProductProductPhotoes on a.ProductPhotoID equals b.ProductPhotoID
join c in context.Products on b.ProductID equals c.ProductID
join d in context.ProductSubcategories on c.ProductSubcategoryID equals d.ProductSubcategoryID
select new SubCateViewModel
{
ProductPhotoID = a.ProductPhotoID,
ThumbnailPhotoFileName = a.ThumbnailPhotoFileName,
ProductID = c.ProductID,
Name = c.Name,
NameofBike = d.NameofBike,
isSelected = d.isSelected,
});
return subcate.AsEnumerable();
}
问题是匿名类型的列表不能转换为SubCateViewModel的列表。
你为什么要使用匿名类型呢?只需在查询中创建一个新的SubCateViewModel实例(选择SubCateViewModel而不是匿名类型)。
在LINQ中使用:
代替匿名类型select new SubCateViewModel
{
// Set class variables here...
}