如何在选择后添加一个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的投影不起作用
答案 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()