【发布时间】:2017-10-19 01:29:53
【问题描述】:
我正在尝试运行 Linq 并引发异常说明:
LINQ to Entities 无法识别方法 '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
我有什么遗漏的吗?
【问题讨论】:
-
所以每个产品都有相同的所有类别列表?
-
你试过直接做Categories = aGateway.GetAllCategories().ToList()吗?
-
其实不是,它们是单独定义的。但我的要求是在侧边栏中显示所有类别和子类别@Equalsk。
-
是的,我已经试过了,不行@gnsanty。