Sql server 具体化查询结果不支持此方法

Sql server 具体化查询结果不支持此方法,sql-server,linq,Sql Server,Linq,我试图使用LINQ Lambda链接许多表,但在处理子查询以获取ProducedBy属性的值时遇到了问题。我收到一条消息:物化查询结果消息不支持此方法 能帮我一下吗 var temp = db.MK_Product_Variant .Join(db.MK_Products, a => a.ProductId, b => b.ID, (a, b) => new { a, b }) .Join(

我试图使用LINQ Lambda链接许多表,但在处理子查询以获取ProducedBy属性的值时遇到了问题。我收到一条消息:物化查询结果消息不支持此方法

能帮我一下吗

            var temp = db.MK_Product_Variant
                .Join(db.MK_Products, a => a.ProductId, b => b.ID, (a, b) => new { a, b })
                .Join(db.MK_Product_Category, c => c.b.CategoryId, d => d.ID, (c, d) => new { c, d })
                .Join(db.MK_Game_Type, e => e.d.GameType, f => f.ID, (e, f) => new { e, f })
                .Where(g=> !db.MK_MP_Variant.Select(h=>h.ID).Contains( g.e.c.a.ID)) /* My Store only */
                .Select(i => new { 
                    Category = i.e.d.Name, 
                    ItemType = i.f.Name, 
                    ItemNo = i.e.c.a.ID, 
                    Enabled = i.e.c.b.Enabled,
                    Status = "", 
                    ProducedBy = (db.MK_Products.Join(db.MK_Production_Resource,k=>k.ID,l=>l.Item,(k,l) => new {k,l})
                    .Join(db.MK_Production_Staff,m=>m.l.Staff,n=>n.ID,(m,n) => new {m,n})
                    .Where(o => o.m.k.ID == i.e.c.a.ID)
                    .Select(p=>p.n.Location).DefaultIfEmpty("").ToList())[0],
                    ReleaseDate = i.e.c.a.ReleaseDate, 
                    ReleasingQty = i.e.c.a.Qty, 
                    Price = i.e.c.a.Price_MKD, 
                    QtyPurchaseFromUser = "", 
                    QtySold = db.MK_Product_VariantHistory.Where(j => j.Variant == i.e.c.a.ID && j.PurchaseDate >= startDate && j.PurchaseDate <= stopDate && !(db.MK_MP_Variant.Select(t => t.ID)).Contains(i.e.c.a.ID)).Count() 
                });

如果您有
select
,在
where
语句之后,您在
select
语句中返回一个匿名类型,该类型的属性名为
ProducedBy
,要设置此属性,您编写了一个查询,最后调用
ToList
,以获取第一个元素。。。 您需要使用
FirstOrDefault
而不是
ToList()
并获得如下零索引元素:

ProducedBy = db.MK_Products
            .Join(db.MK_Production_Resource,k=>k.ID,l=>l.Item,(k,l) => new {k,l})
            .Join(db.MK_Production_Staff,m=>m.l.Staff,n=>n.ID,(m,n) => new {m,n})
            .Where(o => o.m.k.ID == i.e.c.a.ID)
            .Select(p=>p.n.Location)
            .DefaultIfEmpty("")
            .FirstOrDefault()
您不能在linq to entity中调用
ToList
,除非您的日期之前已被提取到内存中,如果您想在主详细信息案例中的linq to entity中填充列表,您可以使用
AsEnumerable

ProducedBy = db.MK_Products
            .Join(db.MK_Production_Resource,k=>k.ID,l=>l.Item,(k,l) => new {k,l})
            .Join(db.MK_Production_Staff,m=>m.l.Staff,n=>n.ID,(m,n) => new {m,n})
            .Where(o => o.m.k.ID == i.e.c.a.ID)
            .Select(p=>p.n.Location)
            .DefaultIfEmpty("")
            .FirstOrDefault()