繁体   English   中英

为什么不能从LINQ子查询联接中选择数据?

[英]Why can't I select data from my LINQ sub query join?

我试图使用左连接嵌套查询从2个表中获取数据。 这使我可以从项目表中获取数据,但不能从购物车(嵌套查询)表中获取数据:

 var q = from s in db.Items
                    join sub in (from c in db.Carts
                                 where c.CartID == 1
                                 group c by c.ItemID into g
                                 select new
                                 {
                                     ItemID = g.Key,
                                     Qty = g.Select(s => s.Qty)

                                 }) on s.ItemID equals sub.ItemID into a
                    select new ItemViewModel
                    {
                        CategoryID = s.CategoryID,
                        Description = s.Description,
                        Price = s.Price,

    **This being the issue------>>>>>>>** //Qty = a.Select(j => j.Qty),

                        ItemID = s.ItemID,
                        ItemName = s.ItemName
                    };


                    viewModel = q.ToList();

我试图实现的查询是:

select Items.*, Cart.Qty
from Items
left join (select ItemID, Qty from carts where CartID = 1 ) Cart 
on Items.ItemID = Cart.ItemID

如果我理解正确,并假定ItemViewModel.Qty属性只是一个int ,则所需查询的最简单形式为:

var q = from item in items
        join cart in
          (from cart in carts where cart.CartID == 1 select cart)
          on item.ItemID equals cart.ItemID into itemCarts
        select new ItemViewModel
        {
            ItemID = item.ItemID,
            Qty = itemCarts.Sum(cart => cart.Qty)
        };

如果你只想稍微修改/修复您的查询:

var q = from s in db.Items
        join sub in (from c in db.Carts
                     where c.CartID == 1
                     group c by c.ItemID into g
                     select new
                     {
                         ItemID = g.Key,
                         Qty = g.Sum(s => s.Qty)
                         // or Qty = g.Select(s => s.Qty)
                         // and below: Qty = a.SelectMany(x => x.Qty).Sum()
                     })
            on s.ItemID equals sub.ItemID into a
        select new ItemViewModel
        {
            CategoryID = s.CategoryID,
            Description = s.Description,
            Price = s.Price,
            Qty = a.Sum(x => x.Qty),
            ItemID = s.ItemID,
            ItemName = s.ItemName
        };

您可以将GroupJoinSelectMany用于LEFT JOIN SQL查询并获取所需的输出。

    var result = db.Items.GroupJoin(db.Carts.Where(x => x.CartID == 1), item => item.ItemID, cart => cart.ItemID, 
                 (item, cart) => new { item, cart })
                .SelectMany(x => x.cart.DefaultIfEmpty(), (it, ca) =>
                {
                    return new ItemViewModel
                {
                        ItemName = it.item.ItemName,
                        Price = it.item.Price,
                        ItemID = it.item.ItemID,
                        // ... .... .... 
                        // Fill the required columns from it.Item property..
                        Qty = ca != null ? ca.Qty : 0
                    };
                }).ToList();

编辑:具有SelectManyLINQ版本。

var result = from s in db.Items
        join sub in (from c in db.Carts
                     where c.CartID == 1
                     select c)
        on s.ItemID equals sub.ItemID into joined
        from row in joined.DefaultIfEmpty()
        select new ItemViewModel 
        { 
            CategoryID = s.CategoryID,
            Description = s.Description,
            Price = s.Price,
            Qty = row != null ? row.Qty : 0,
            ItemID = s.ItemID,
            ItemName = s.ItemName
        };

C#小提琴与示例数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM