Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq Select带有内部方法错误-Linq to实体无法识别该方法_C#_Linq_Entity Framework 4 - Fatal编程技术网

C# Linq Select带有内部方法错误-Linq to实体无法识别该方法

C# Linq Select带有内部方法错误-Linq to实体无法识别该方法,c#,linq,entity-framework-4,C#,Linq,Entity Framework 4,我正在为我的项目使用视图/演示者结构。该项目是一个包含OrderedItems和产品的电子通信站点。 调用GetOrdersItemsByOrderID(orderID)时,我得到一个LINQ错误 LINQ to Entities无法识别方法“DAL.Views.ProductView GetProductBySku(System.String)”方法,并且无法将此方法转换为存储表达式 我知道LINQ不喜欢其查询中的dynamc内容,所以问题是SELECT Product=Presenters.

我正在为我的项目使用视图/演示者结构。该项目是一个包含OrderedItems和产品的电子通信站点。 调用GetOrdersItemsByOrderID(orderID)时,我得到一个LINQ错误

LINQ to Entities无法识别方法“DAL.Views.ProductView GetProductBySku(System.String)”方法,并且无法将此方法转换为存储表达式

我知道LINQ不喜欢其查询中的dynamc内容,所以问题是SELECT Product=Presenters.ProductsPresenter.GetProductBySku(c.productSKU)

我不知道如何构建GetOrdersItemsByOrderID TOLIST,同时还有另一个GetProductBySku方法填充字段ProductDetail

/----------------/

/----------------/


/----------------/

在调用该方法之前,您可以添加一个“ToList”来执行查询

试试这个:

   var result = ctx.OrderedItems.Where(o => o.orderID == orderID).ToList()
        .Select(c => new OrderedItemsView
                         {
                             OrderItemID = c.orderItemID,
                             OrderID = c.orderID,
                             ProductSku = c.productSKU,
                             ProductPrice = c.productPrice,
                             ProdQuantity = c.prodQuantity,
                             ProductDetail = Presenters.ProductsPresenter.GetProductBySku(c.productSKU)
                         });

我把你的
ToList()
调高了一点。您可能还需要在选择的末尾添加一个。

Yikes。如果
OrderedItems
听起来像这样,那么这段代码将在任何实际数据上消失。我假设它位于“where”之后的.ToList()将被适当地限制。完美。。移动ToList后,我需要返回'results.ToList()'。。谢谢你应该使用
.AsEnumerable()
,而不是
.ToList()
@Robaticus:你应该在最后使用
.ToList()
,在所有
的Where
之后,
选择
等等,并且只有当你确实需要一个列表的时候。但是要从SQL查询转换到C代码,您应该使用
AsEnumerable
。否则,生成该列表只是为了在其上实例化另一个迭代器(
Select
),您可能需要从中创建另一个列表。那是多余的。在技术术语中,
AsEnumerable
维护惰性评估。
public class OrderedItemsView
    {
        public int OrderItemID { get; set; }
        public int OrderID { get; set; }
        public string ProductSku { get; set; }
        public decimal ProductPrice { get; set; }
        public int ProdQuantity { get; set; }
        public decimal? ProductWeight { get; set; }

      public ProductView ProductDetail { get; set; } <-- Get Product Details
    }
public static Views.ProductView GetProductBySku(string sku)
    {
    using (GroupProductEntities ctx = new GroupProductEntities())
    {
        //-------------------------------------------------------------------//
        var results =
            ctx.Products.Where(p => p.clientID == Config.ClientID && p.productSKU == sku)
            .Select(p => new Views.ProductView
                             {
                                 ProductID = p.productID,
                                 ClientID = p.clientID,
                                 CategoryID = p.categoryID,
                                 ProductName = p.productName,
                                 ProductImage1 = p.productImage1,
                                 ProductPrice = p.productPrice,
                                 ProductInventory = p.productInventory,
                                 ProductSku = p.productSKU,
                             }).FirstOrDefault();
        return results;
    }
 }
public class ProductView
{
    public int ProductID { get; set; }
    public int? CategoryID { get; set; }
    public string ProductName { get; set; }
    public string ProductShortDesc { get; set; }
    public string ProductImage1 { get; set; }
    public decimal? ProductPrice { get; set; }
    public decimal? ProductInventory { get; set; }
    public string ProductSku { get; set; }
}
   var result = ctx.OrderedItems.Where(o => o.orderID == orderID).ToList()
        .Select(c => new OrderedItemsView
                         {
                             OrderItemID = c.orderItemID,
                             OrderID = c.orderID,
                             ProductSku = c.productSKU,
                             ProductPrice = c.productPrice,
                             ProdQuantity = c.prodQuantity,
                             ProductDetail = Presenters.ProductsPresenter.GetProductBySku(c.productSKU)
                         });