2014-03-30 95 views
1

我想在我的EntitySetController從我的Get方法返回之前訂購的集合,這裏是我的代碼:返回從EntitySetController有序集合獲取

[Queryable] 
    public IQueryable<Standing> GetStandings() 
    { 
     //order by points, goalsfor-goalsagainst, goalsfor, teamname 
     IQueryable<Standing> standings = db.Standings.Include("Team").Include("Stage"); 

     var standingsQuery = from s in standings 
      let points = (s.Won*3) + (s.Drawn*1) 
      select standings.OrderByDescending(p => points) 
       .ThenByDescending(g => g.GoalsFor - g.GoalsAgainst) 
       .ThenBy(t => t.Team.TeamName); 

     return standingsQuery.AsQueryable(); 

    } 

但我得到的錯誤:

Cannot implicitly convert type 'System.Linq.IQueryable<System.Linq.IOrderedQueryable<Standing>>' to 'System.Linq.IQueryable<Standing>'. An explicit conversion exists (are you missing a cast?) 

我是否需要單獨的方法來返回有序集合?

回答

1

嘗試這樣的:

[Queryable] 
public IQueryable<Standing> GetStandings() 
{ 
    //order by points, goalsfor-goalsagainst, goalsfor, teamname 

    IQueryable<Standing> standings = db.Standings.Include("Team").Include("Stage"); 

    IOrderedQueryable<Standing> standingsQuery = standings 
     .OrderBy(s => s.Won * 3 + s.Drawn * 1) 
     .ThenByDescending(s => s.GoalsFor - s.GoalsAgainst) 
     .ThenBy(s => s.Team.TeamName); 

    return standingsQuery.AsQueryable(); 
} 

爲什麼你的代碼不起作用的原因是因爲你是返回一個IQueryable<IOrderedQueryable<Standing>>而不是IOrderedQueryable<Standing>

+0

謝謝!我只需將IOrderedEnumerable更改爲IOrderedQueryable即可運行 – user517406

+0

是的,您是正確的。我犯了一個錯誤。 –