查询不为orderby投影?

时间:2014-01-31 00:01:39

标签: linq entity-framework

如何在选择后添加一个orderby?

//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;

它与b的投影不起作用

2 个答案:

答案 0 :(得分:2)

您必须在OrderBy之前致电Select,因为在投影后,您尝试订购的列不再可用:

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

答案 1 :(得分:0)

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