C# – How to sort a list


I have a datatable which I will convert to a list<datarow>. How do I sort the list based on some datatable column? I think it's something like list = list.Sort(p=>p.Field() but I am not sure about the syntax.

I am interested in using LINQ heavily so I was wondering if I should convert the datatable to a strongly typed generic list and take it from there instead of using linq to datasets. The only reason I didn't do this now is for performance reason. The datatable consists of about 20,000 records.

Best Solution

I'd recommend you covert the dataset to a generic list and use linq to sort:

var collection =
            from c in people
            orderby c.Name ascending
            select c;

return collection.ToList();

Where people is the generic List. There are other ways to sort using linq.

Are you always going to return 20k records? The way I understand it, there's more overhead with datatables/sets/rows than with generic lists due to all the built-in .NET functions and methods...but I might be wrong.