It's a beautiful, beautiful world with LINQ and Lambda Expressions!

by Dmitry [dimaka] Pavlov 10. March 2010 23:33

I like C# for a long time. And now I like it even more once I have tried LINQ extension introduced in .NET Framework 3.5. And even more with combination LINQ with Lambda Expressions (anonymous functions).

I won't write ABOUT a lot, just some lines of code:

Example 1: Filter and sort collection

List<Entry> entries = Entries.FindAll(
 e => e.IsPublished && e.Type == EntryType.NewsPost);
entries.Sort((x, y) => y.DateCreated.CompareTo(x.DateCreated));

Example 2: Filling DropDownList control with items

ddlParent.Items.AddRange(Entry.Entries.FindAll(e => e.Id != id)
    .ConvertAll(e => new ListItem(e.Title, e.Id.ToString())).ToArray());

I like it!

Tags:

.NET | Tips | Lambda | LINQ

Comments

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar