Answered by:
Linq to objects GroupBy OrderBy

Question
-
I am trying to do the linq eqivalent of the following
SELECT a, b, sum(c)
FROM table
GROUP BY a,b
ORDER BY a,b
I can find no examples of how to greate a two level group by then apply a two level order by. This is trivial in SQL. What am I missing?Monday, January 25, 2010 8:48 PM
Answers
-
Good morning JRichTX,
Welcome to Visual C# Language forum!
The LINQ equivalent of your SQL query could be like this one:
======================================================================================
var query = from t in table
group t by new { t.a, t.b } into list
orderby list.Key.a, list.Key.b
select new { list.Key.a, list.Key.b, sum = list.Sum(tt => tt.c) };
======================================================================================
Here, the GroupBy query is based on an anonymous type { a, b }. The OrderBy query needs to be executed as the last statement before the final select clause because other operations might break the order. The above LINQ query returns an another anonymous type { a, b, Sum }.
If you have any questions, please feel free to let me know.
Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Tuesday, January 26, 2010 1:31 AMModerator
All replies
-
Good morning JRichTX,
Welcome to Visual C# Language forum!
The LINQ equivalent of your SQL query could be like this one:
======================================================================================
var query = from t in table
group t by new { t.a, t.b } into list
orderby list.Key.a, list.Key.b
select new { list.Key.a, list.Key.b, sum = list.Sum(tt => tt.c) };
======================================================================================
Here, the GroupBy query is based on an anonymous type { a, b }. The OrderBy query needs to be executed as the last statement before the final select clause because other operations might break the order. The above LINQ query returns an another anonymous type { a, b, Sum }.
If you have any questions, please feel free to let me know.
Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Tuesday, January 26, 2010 1:31 AMModerator -
Hello JRichTX,
How is the problem now?
If you need any further assistance, please feel free to let me know.
Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Thursday, January 28, 2010 12:54 AMModerator -
I apologize for not letting your know that your answer was PRECISELY what I needed. Thank you very much for your helpSaturday, January 30, 2010 2:57 PM
-
It's my pleasure! :)
Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Monday, February 1, 2010 1:05 AMModerator