bitshift
bitshift

Reputation: 6842

Query not projecting for oderby?

How can I add an orderby after the select?

//what I have now
string country_list = string.Join(":", ctx.Countries.Select(a => a.CountryName).ToArray());

return country_list;

//what I want to do, but the orderby doesnt see the projections
string country_list = string.Join(":", ctx.Countries.Select(a => a.CountryName).OrderBy(b => b.StateId).ToArray());

return country_list;

its the projection with b that isnt working

Upvotes: 0

Views: 38

Answers (2)

Amir Sherafatian
Amir Sherafatian

Reputation: 2083

ctx.Countries.Select(a => new { a.CountryName, a.StateId })
             .OrderBy(b => b.StateId)
             .ToArray()

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

You have to call OrderBy before Select, because after projection the column you're trying to order by is no longer available:

string country_list = string.Join(":", ctx.Countries.OrderBy(b => b.StateId).Select(a => a.CountryName).ToArray());

Upvotes: 2

Related Questions