Wednesday, April 21, 2010

LINQ Examples

Some basic LINQ examples for queering into collections.LINQ extension here: msdn

var newThings = from thing in allThings
                 where thing.Stamp >= dateYesterday
                 select thing;

extracting different collection structure:

IEnumerable<string> ids = mUsers.Select(u => u.GUID);

getting unique ones:

IEnumerable<string> uniqueIds = ids.Distinct<string>();

finding one:

IEnumerable<User> users = mUsers.Where(u => u.GUID == thatGUID);

flattens the resulting sequences into one sequence

IEnumerable<Locations> places = mUsers.SelectMany(u => u.Locations);

0 comments:

Post a Comment