The basics of processing linq with group

results  = mylist
.GroupBy(x =>x.id)
.Select(x =>
{
var myItem = x.First();
string item_aggregated = String.Join(“,”, x.Select( … );
})
     .AsEnumerable();

Or, as stated here http://stackoverflow.com/questions/7325278/group-by-in-linq

var results = from p in persons
              group p.car by p.PersonId into g
              select new { PersonID = g.Key, Cars = g.ToList() };

Or as a non-query expression:

var results = persons.GroupBy(p => p.PersonId, p => p.car,
                         (key, g) => new { PersonId = key, Cars = g.ToList() });

Basically the contents of the group (when view as an IEnumerable<T>) is a sequence of whatever values were in the projection (p.car in this case) present for the given key.

Also: http://codeblog.jonskeet.uk/2011/01/01/reimplementing-linq-to-objects-part-21-groupby/

 

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s