[asp.net-mvc]返回从 EntitySetController 得到的有序的集合

发布时间: 2016/10/9 6:04:58
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想订购前从我在我的 EntitySetController 的 Get 方法返回一个集合,这里是我的代码︰

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

赞助商