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

时间:2018-07-19 12:06:05

标签: c# asp.net-mvc entity-framework linq lambda

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

foreach($link in $gitlink)
{
    $s = $link
    $s.Substring(0, $s.IndexOf('/'))
    echo $s
}

我要实现的查询是:

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

2 个答案:

答案 0 :(得分:1)

如果我理解正确,并且假设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
        };

答案 1 :(得分:1)

您可以将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();

编辑:带有LINQ的{​​{1}}版本。

SelectMany

带有示例数据的C# Fiddle