Monday, February 4, 2008

Dynamically sorting your data easily using Linq & Lamda

Here is 1 line of code to sort data dynamically based on sorting parameter A

YourProducts.OrderByDescending(p => p.GetType().GetProperty(A.GetValue(c,null))

It will get your property value of Product class dynamically.

Here are some example to use lamda expression in C# 3.0:

x => x + 1 // Implicitly typed, expression body

x => { return x + 1; } // Implicitly typed, statement body

(int x) => x + 1 // Explicitly typed, expression body

(int x) => { return x + 1; } // Explicitly typed, statement body

(x, y) => x * y // Multiple parameters

() => Console.WriteLine() // No parameters

No comments: