Thoughts from Dan Miser RSS 2.0
 Tuesday, January 05, 2010
Consider this code fragment:
var list =
from tx in Transactions
group tx by new {tx.TransactionType, tx.ClientTypeID};

foreach (var item in list)
{
Console.WriteLine(item.Key.TransactionType);
Console.WriteLine(item.Sum(tx => tx.Amount));
foreach (var item2 in item)
Console.WriteLine(item2.TransactionID);
}

After executing the query, we now have an IGrouping<AnonymousType, Transaction> that lets us run through all of the grouped objects, while still having access to the original objects in the inner loop. We can accomplish the exact same thing with this alternative LINQ statement, but the first version seems cleaner to me:

var list =
from tx in Transactions
group tx by new {tx.TransactionType, tx.ClientTypeID} into g
select g;
Tuesday, January 05, 2010 10:30:01 AM (Central Standard Time, UTC-06:00)  #    Comments [0] -
.NET | LINQ
Navigation
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Dan Miser
Sign In
Statistics
Total Posts: 338
This Year: 4
This Month: 0
This Week: 0
Comments: 613
All Content © 2010, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)