C# 是否可以在一个LINQ查询中完成所有这一切?

C# 是否可以在一个LINQ查询中完成所有这一切?,c#,asp.net,asp.net-mvc,algorithm,linq,C#,Asp.net,Asp.net Mvc,Algorithm,Linq,我有一个这样的函数 private List<Score> getPageNRows ( int N ) { // Returns object corresponding to the rows of the table // on "page" N of the scores page return (from s in this._SD.Scores orderby s.score1 descending

我有一个这样的函数

private List<Score> getPageNRows ( int N )
{
    // Returns object corresponding to the rows of the table
    // on "page" N of the scores page
    return (from s in this._SD.Scores
            orderby s.score1 descending
            select s)
            .Skip(ScoresController._scoresPerPage * (N - 1))
            .Take(ScoresController._scoresPerPage * N)
            .ToList();
}
return this._SD.Scores
            .OrderByDescending(s => s.score1)
            .Skip(ScoresController._scoresPerPage * (N - 1))
            .Take(ScoresController._scoresPerPage * N))
            .Select(s => new ViewScore { score = s.score1, name = s.name, datestr = s.playdate.ToString() })
            .ToList();      
在这里,我真正想要的是一个
列表,其中
ViewScore

public class ViewScore
{
    public int score { get; set; } // corresponds directly to score1 in Score class
    public string name { get; set; } // corresponds directly to name in Score
    public string datestr { get; set; } // corresponds to playdate.ToString()
}
这可以在LINQ查询中完成所有操作,还是需要创建帮助器方法


至少,我如何只选择列
s.score1
s.name
s.playdate
,而不是全部(通过
select
)?

我建议使用lambda路径:

private List<Score> getPageNRows ( int N )
{
    // Returns object corresponding to the rows of the table
    // on "page" N of the scores page
    return this._SD.Scores.OrderByDescending(c => c.score1)
                          .Skip(ScoresController._scoresPerPage * (N - 1))
                          .Take(ScoresController._scoresPerPage * N)
                          .ToList();
}

可以使用投影仅返回选定的列。方法
Select()
用于项目:

return (from s in this._SD.Scores
                orderby s.score1 descending
                select s
                ).Skip(ScoresController._scoresPerPage * (N - 1)
                ).Take(ScoresController._scoresPerPage * N)
                 .Select(x => new ViewScore() { score = x.score1, name = x.name, datestr = x.playdate  })
                 .ToList();

使用
ToList()
在查询具体化之前使用
Select()
非常方便,可以将从DB返回的数据限制为真正需要的数据。

是的,您可以使用
Linq
这样做

private List<Score> getPageNRows ( int N )
{
    // Returns object corresponding to the rows of the table
    // on "page" N of the scores page
    return (from s in this._SD.Scores
            orderby s.score1 descending
            select s)
            .Skip(ScoresController._scoresPerPage * (N - 1))
            .Take(ScoresController._scoresPerPage * N)
            .ToList();
}
return this._SD.Scores
            .OrderByDescending(s => s.score1)
            .Skip(ScoresController._scoresPerPage * (N - 1))
            .Take(ScoresController._scoresPerPage * N))
            .Select(s => new ViewScore { score = s.score1, name = s.name, datestr = s.playdate.ToString() })
            .ToList();      

@大卫贝茨:你这是什么意思?别担心,其他人(如阿尔吉亚·C)也说过与我相同的话,但这是一个可靠的回答。:)当前上下文中不存在名称
s