2014-01-31 68 views
0

如何在選擇後添加orderby?查詢不投影oderby?

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

你必須Select之前調用OrderBy,因爲投影后不再可用你試圖用命令列:

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

我知道它必須是簡單的東西,謝謝! – bitshift

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