Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying the sort order of a LINQ query

Tags:

linq

I expected the following LINQ query to sort according to FirstName but the OrderBy extension method seems to have no effect.

DataClassesDataContext dc = new DataClassesDataContext();

var query = from contact in dc.Contacts
            select contact;

query.OrderBy(c => c.FirstName);

Everything works fine when I include the orderby in the initial query definition but I want to be able to modify it based on conditions later in my code.

Any idea why this isn't working?

like image 759
joshb Avatar asked Nov 29 '22 05:11

joshb


1 Answers

try using

query = query.OrderBy(c => c.FirstName);

or

var sortedQuery = query.OrderBy(c => c.FirstName);

The OrderBy creates a new sequence.

like image 57
Mike Two Avatar answered Jun 02 '23 13:06

Mike Two