-1

I got an inventory table and a stocktransactions table. Items from the inventory can be either sold or freely distributed. I want to write a query that retrieves for each item the total sold, the total distributed for free and the generated revenue for the sale transactions.

something along the lines of :

select i.Id,i.Title,
Sum(case when transactiontype=1 then s.MovedQuantity else 0 end) sold,

Sum(case when transactiontype=1 then s.MovedQuantity*s.UnitPrice else 0 end) Revenue,

Sum(case when transactiontype=0 then s.MovedQuantity else 0 end) distributed

From Inventory i,StockTransactions s

where i.Id=s.ItemId

group by i.Id,i.Title

how can this be done with linq/lambda?

6
  • try this: stackoverflow.com/questions/15577890/… Commented Aug 12, 2013 at 16:17
  • I know how to use lambda with linq. I just need help translating this sql into a valid expression Commented Aug 12, 2013 at 16:21
  • Which part in particular are you having difficulty with? GroupBy, Select and Sum should do the trick in the right combination. Commented Aug 12, 2013 at 16:26
  • What is transactiontype? Commented Aug 12, 2013 at 16:31
  • it's an int field that specifies whether the transaction is a sale,entry into stock or free copy distribution transaction Commented Aug 12, 2013 at 16:36

2 Answers 2

0

This may be what you need:

var result = Inventory.Join(StockTransactions, x=>x.Id, x=>x.ItemId,(x,y)=>new {x,y})
                      .GroupBy(a=>new {a.x.Id, a.x.Title})
                      .SelectMany(a=>
                         new {
                                a.Key.Id,
                                a.Key.Title,
                                sold = a.Sum(b=> b.y.transactiontype == 1 ? b.y.MovedQuantity : 0),
                                Revenue = a.Sum(b=>b.y.transactiontype == 1 ? b.y.MovedQuantity * b.y.UnitPrice : 0),
                                distributed = a.Sum(b=> b.y.transactiontype == 0 ? b.y.MovedQuantity : 0)                       
                             });
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, it is exactly what i needed. I simply replaced SelectMany with Select to make it work.
0

Try this

 var result = ((from inv in inventry.AsQueryable()
                      join st in stockTransactions.AsQueryable()
                      on inv.Id equals st.Id
                      select new { ID = inv.Id, Title = inv.Title, MovedQuantity = st.MovedQuantity, UnitPrice = st.UnitPrice })
                      .GroupBy(s=>new {s.ID,s.Title})
                      .Select(res=>new {Id=res.Key.ID,Title=res.Key.Title,Sold=transactiontype==1?res.Sum(s=>s.MovedQuantity):0,
                      Revenue=transactiontype==1?res.Sum(s=>s.MovedQuantity*s.UnitPrice):0,
                      Distributed=transactiontype==1?res.Sum(s=>s.MovedQuantity):0})).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.