【问题标题】:Error in Linq - Unable To Translate into Store ExpressionLinq 中的错误 - 无法转换为商店表达式
【发布时间】:2017-10-19 01:29:53
【问题描述】:

我正在尝试运行 Linq 并引发异常说明:

LINQ to Entities 无法识别方法 'System.Collections.Generic.List`1[ECommerceApp.Models.CategoryList] CategoryDe​​tails()' 方法,并且该方法无法转换为商店表达式。

以下是定义的类:

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。

标签: c# linq


【解决方案1】:

这样的事情应该可以工作:

  var result = (from c in db.Products
                  select new ProductDetails
                  {
                     ProductName = c.ProductName,
                     Price = c.Price,
                     Categories = null 
                  }).ToList();
 foreach (var c in result)
     {
       c.Categories= CategoryDetails();
     }
     return View(result);

如果这不起作用,请定义结果类,并且不要在 linq 查询中分配类别属性

【讨论】:

  • 工作完美@gnsanty。非常感谢。
【解决方案2】:

表达式的查询端不知道您可用的 CategoryDetails() 函数。

A similar issue with suggestions

【讨论】:

    【解决方案3】:

    尝试先在查询之外获取类别并将它们存储在集合中,然后将集合分配给新创建的 ProductDetails

    public ActionResult Index()
    {
        var cats = CategoryDetails();
        var result = (from c in db.Products
                      select new ProductDetails
                      {
                         ProductName = c.ProductName,
                         Price = c.Price,
                         Categories = cats 
                      }).ToList();
    
         return View(result);
    }
    
    public List<CategoryList> CategoryDetails()
    {
        List<CategoryList> result = aGateway.GetAllCategories().ToList();
        return result;
    }
    

    【讨论】:

      【解决方案4】:

      gnsanty 回答有效,但如果您使用的是 linq,则在这种情况下没有理由使用 foreach 语句:

      var result = (from c in db.Products
                    select new ProductDetails
                    {
                       ProductName = c.ProductName,
                       Price = c.Price,
                       Categories = null 
                    })
        .ToList()  // Execute the query before updating the property
        .Select(pd => {
          pd.Categories = CategoryDetails();
          return pd;
        })
        .ToList();
      

      【讨论】:

      • 啊!谢谢@Erik Philips。我非常感谢您的回答,然后运行它。
      • gnsanty 的回答绝对没有问题,如果可以的话,我只是更喜欢留在 linq 语句中。我觉得这更容易阅读,但这只是我的意见。我怀疑编译后有什么不同。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多