Thursday, December 8, 2011

Use Linq to obtain a unique list of properties from a list

Use the Distinct operator:
var idList = yourList.Select(x=> x.ID).Distinct();

Wednesday, December 7, 2011

Linq calculate average of a list C#

Only one line of code will do it:

List<float> f = new List<float>();
for (int i = 0; i < 10; i++)
{
    f.Add((float)i);
}
float ave = f.Aggregate((acc, cur) => acc + cur) / f.Count;
Console.WriteLine(ave);