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

Answers


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());

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

Need Your Help

How to compare local with remote git repo in PhpStorm?

git phpstorm

I am stuck on how to compare local files to files in remote repo.