Convert simple SQL group-by into LINQ to SQL?

It looks to me like you want: Select(g => new { ProductName = g. Key, TotalOrdered = g. Sum(x => x.

NumberOf) }) You can do your whole query as either a single query expression or without using query expressions at all though: var totalProducts = ctx. Orders . Join(ctx.

Products, o => o. ProductId, p => p. Id, (o, p) => new { p.Name, o.

NumberOf }) . GroupBy(t => t.Name, pair => pair. Name, // Key selector pair => pair.

NumberOf, // Element selector (key, numbers) => new { ProductName = key, TotalOrdered = numbers.Sum()) }) Or: var totalProdcuts = from o in ctx. Orders join p in ctx. Products on o.

ProductId equals p.Id group o. NumberOf by p. Name into g select new { ProductName = g.

Key, TotalOrdered = g.Sum() }.

It looks to me like you want: . Select(g => new { ProductName = g. Key, TotalOrdered = g.

Sum(x => x. NumberOf) }) You can do your whole query as either a single query expression or without using query expressions at all though: var totalProducts = ctx. Orders .

Join(ctx. Products, o => o. ProductId, p => p.Id, (o, p) => new { p.

Name, o. NumberOf }) . GroupBy(t => t.

Name, pair => pair. Name, // Key selector pair => pair. NumberOf, // Element selector (key, numbers) => new { ProductName = key, TotalOrdered = numbers.Sum()) }); Or: var totalProdcuts = from o in ctx.

Orders join p in ctx. Products on o. ProductId equals p.

Id group o. NumberOf by p.Name into g select new { ProductName = g. Key, TotalOrdered = g.Sum() }.

This is correct. Have just checked. Thank you for helping me in my brain-dead moment.

– Lisa Jul 7 at 6:01 @Lisa: Okay - will edit further... – Jon Skeet Jul 7 at 6:03 In response to your update, I am already grouping by Name. – Lisa Jul 7 at 6:03 @Lisa: Your SQL originally grouped by NumberOf as well... anyway, see my latest edit. – Jon Skeet Jul 7 at 6:10 Thanks for quick response and how it is done properly without mixing syntaxes as I did -- I just got stuck.

Nice to see it can be done entirely with either but a single query expression would be the choice for this task. – Lisa Jul 7 at 6:33.

TotalOrdered = g. Sum(o => o. NumberOf) make use of above for .... than statement might be select new { ProductName= g.

Key, TotalOrdered = g. Sum(o => o. NumberOf) }; var query = from o in ctx.

Orders join p in ctx. Products on o. ProductId equals p.Id group o by new { p.

Name, o. NumberOf } into g select new { ProductName= g. Key, TotalOrdered = g.

Sum(o => o. NumberOf) }.

I understand how to obtain the sum. What was confusing me was how to get it into a projection. – Lisa Jul 7 at 6:05 @Lisa - try now – Pranay Rana Jul 7 at 6:11 Yes this also works perfectly.

I think it was what I was looking for in the beginning. It's much more elegant than by mix of LINQ-to-SQL and Linq extensions. – Lisa Jul 7 at 6:14 @Lisa - okies no issues....getting +1 when john post answer its my pleasure....... – Pranay Rana Jul 7 at 6:15.

Just pasting this here in case it is helpful to know how this could be achieved through GroupJoin method: var totalProducts = ctx.Products. GroupJoin(ctx. Orders, outerKeySelProduct => outerKeySelProduct.Id, innerKeySelOrder => innerKeySelOrder.

ProductId, (outerKeySelProduct, innerKeySelOrder) => new { ProductName = outerKeySelProduct. Name, TotalOrdered = innerKeySelOrder. Select(n => n.

NumberOf).Sum() }); Happy for it to be edited if it can be improved.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions