我试图运行Linq
,它抛出一个异常,指出:错误Linq中 - 无法翻译成商店表达
LINQ到实体无法识别方法“System.Collections.Generic.List` 1 [ECommerceApp.Models.CategoryList] CategoryDetails()'方法,并且此方法不能转换为商店表达式。
以下是定义的类:
public class ProductDetails : Products
{
public string ParentCatName { get; set; }
public string ChildCatName { get; set; }
public string ImagePath { get; set; }
public List<CategoryList> Categories { get; set; }
}
public class CategoryList
{
public string Category { get; set; }
public string SubCategory { get; set; }
public string ParentCategory { get; set; }
}
我已经显示在主页上的所有类别,并在类属的,因此,定义的列表。以下是Linq
:
public ActionResult Index()
{
var result = (from c in db.Products
select new ProductDetails
{
ProductName = c.ProductName,
Price = c.Price,
Categories = CategoryDetails() //Throws exception here and I tried to convert it to ToList() but doesn't work
}).ToList();
return View(result);
}
public List<CategoryList> CategoryDetails()
{
List<CategoryList> result = aGateway.GetAllCategories().ToList();
return result;
}
最后在视图中,我试图访问类的细节与foreach
循环如下:
@item.Categories[0].Category
@item.Categories[0].SubCategory
@item.Categories[0].ParentCategory
这有什么,我很想念?
所以每个产品只分配了所有类别的同一个列表? – Equalsk
你有没有试过直接做Categories = aGateway.GetAllCategories()。ToList()? – Homerothompson
其实不是,它们是单独定义的。但我的要求是在侧边栏@Equalsk中显示所有类别和子类别。 –