how to convert sql union to linq

前端 未结 3 2100
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 20:44

I have the following Transact SQL query using a union. I need some pointers as to how this would look in LINQ i.e some examples wouldbe nice or if anyone can recommend a goo

3条回答
  •  星月不相逢
    2020-12-30 21:23

    There are the 101 Linq Samples - with two union samples Union1 and Union2

    This Linq statement should get you the same results as your SQL: (it has for me on a test record-set)

    var results = (from a in (from d in DiscountPromotions
                group d by d.BarCode into g
                select new { 
                    BarCode = g.Key,
                    AmountTaken = g.Sum(p => p.AmountTaken)
                    }).Union(from i in ItemSaleTransactions
                group i by i.BarCode into o
                select new { 
                    BarCode = o.Key,
                    AmountTaken = o.Sum(i => i.AmountTaken)
                    }) group a by a.BarCode into b
                    select new {
                        BarCode = b.Key,
                        AmountTaken = b.Sum(c => c.AmountTaken)
                    });
    

提交回复
热议问题