【问题标题】:Why can't I select data from my LINQ sub query join?为什么我不能从我的 LINQ 子查询连接中选择数据?
【发布时间】:2018-07-19 12:06:04
【问题描述】:

我正在尝试使用左联接从 2 个表中获取数据到嵌套查询。这使我可以从 Item 表中获取数据,但不能从购物车(嵌套查询)表中获取数据:

 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,但给出的错误是无法将 system.Collections.Generic.IEnumberable 转换为 int。
  • Select 总是返回一个集合。如果要将其折叠成单个值,则需要使用适当的折叠函数:CountSumAggregate 等。

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


【解决方案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();

编辑:带有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# Fiddle

【讨论】:

  • 我很确定 OP 要求提供内连接解决方​​案,我认为这里代表左外连接。
  • 正确的是 LEFT JOIN 但请检查 OP 中的 SQL。它被明确提到为LEFT JOIN。他需要物品,但如果 Qty 出现在购物车中,那也是。不是INNER JOIN
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多