Monday, December 14, 2009

Small example of LINQ in C# to sort lists of structures

Imagine we have a list of things and we want to display them in a certain order.
Suppose we have a structure named Attribute that contains the Name and Surname variables. Now we create a list of structures named AttributeList and we want to sorted by Name, then we can do:

var sorted = (from att in AttributeList orderby att.Name ascending select att);

foreach (Attribute att in sorted)
Console.WriteLine(att.Name);


And the same for the case of order by Surname.

I hope this is helpful.

No comments:

Post a Comment