【问题标题】:Returning ordered collection from EntitySetController Get从 EntitySetController Get 返回有序集合
【发布时间】:2014-03-30 17:54:03
【问题描述】:

我试图在从我的 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?)

我需要一个单独的方法来返回一个有序的集合吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-web-api odata


    【解决方案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&lt;IOrderedQueryable&lt;Standing&gt;&gt; 而不是IOrderedQueryable&lt;Standing&gt;

    【讨论】:

    • 谢谢!我只需要将 IOrderedEnumerable 更改为 IOrderedQueryable 即可使其工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 2019-06-28
    • 2013-12-24
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    相关资源
    最近更新 更多