简体   繁体   中英

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

I am trying to get data from 2 tables using a left join to a nested query. This allows me to get data from Item table but not the cart(nested query) table:

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

The query i am trying to acheive is:

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

If I'm understanding correctly, and assuming that ItemViewModel.Qty property is just an int , the simplest form of the query you want is:

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

If you want to only slightly modify/fix your query:

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
        };

You can use GroupJoin with SelectMany for LEFT JOIN SQL Query and get the desired output.

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

EDIT: The LINQ version with SelectMany .

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
        };

The C# Fiddle with sample data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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