是否可以在一个LINQ查询中完成所有这些操作

本文关键字:查询 LINQ 操作 所有这些 一个 是否 | 更新日期: 2023-09-27 18:19:56

我有一个类似的函数

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();
}

其中Score由定义

public partial class Score
{
    public Score()
    {
        GameLogs = new HashSet<GameLog>();
    }
    public int id { get; set; }
    [Column("score")]
    public int score1 { get; set; }
    [StringLength(50)]
    public string name { get; set; }
    public DateTime playdate { get; set; }
    public virtual ICollection<GameLog> GameLogs { get; set; }
}

这里,我真正想要的是List<ViewScore>,其中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.score1s.names.playdate,而不是全部(通过select)?

是否可以在一个LINQ查询中完成所有这些操作

是的,你可以像这个一样使用Linq

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();      

可以使用投影只返回选定的列。方法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返回的数据限制为真正需要的数据。

我建议使用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();
}

根据MSDN,查询语法中不支持skip和take:请参阅此处。

看到这个堆栈溢出问题也提出了类似的问题。

现在,如果您想将Score类投影到ViewScore类中,只需添加一个Select<TSource, TResult>语句:

private List<ViewScore> 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)
                          .Select(c => new ViewScore()
                                       {
                                           score = c.score1,
                                           name = c.name,
                                           datestr = c.playdate.ToString()
                                       })
                          .ToList();
}