2

我正在尝试使用 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() 
                });

这是我要使用它的代码行:

            int count = 0;

            foreach (var item in temp)
            {
                result.Add(count, new string[] { item.Category, item.ItemType, item.ItemNo.ToString(), item.Enabled.ToString(), "", item.ProducedBy.Count()==0?"":item.ProducedBy.FirstOrDefault().Country , string.Format("{0:yyyy-MM-dd hh:mm:ss tt}", item.ReleaseDate), "", "", item.ReleasingQty.ToString(), "", item.QtySold.ToString(), item.QtyPurchaseFromUser.ToString(), string.Format("{0:#0}", item.Price), "", "", "", "", "" });

                count++;
            }
4

1 回答 1

1

你有select,在where语句之后,你返回一个匿名类型的select语句,它有一个名为的属性ProducedBy,设置这个属性你已经写了一个查询,最后,调用ToList了第一个元素......你需要使用它FirstOrDefaultToList()获得零索引像这样的元素:

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()

您不能ToList在 linq to entity 之间调用,除非您的日期之前被提取到内存中,如果您想在主详细信息案例中填充 linq to entity 之间的列表,您可以AsEnumerable使用

于 2014-01-29T19:52:33.487 回答