【问题标题】:Is it possible to do all this in a single LINQ query?是否可以在单个 LINQ 查询中完成所有这些操作?
【发布时间】:2016-01-31 15:02:28
【问题描述】:

我有这样的功能

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&lt;ViewScore&gt;,其中 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)?

【问题讨论】:

  • @DavidBetz 你这是什么意思?
  • 没关系,其他人(例如 Arghya C)和我说的一样,但作为一个可靠的答案。 :)

标签: c# asp.net asp.net-mvc algorithm linq


【解决方案1】:

是的,您可以像这样使用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();      

【讨论】:

    【解决方案2】:

    可以使用投影仅返回选定的列。投影使用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() 可以非常方便地将从数据库返回的数据限制为真正需要的数据。

    【讨论】:

      【解决方案3】:

      我建议使用 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:See Here

      看到这个stack overflow question 询问类似的事情。

      现在,如果您想将 Score 类投影到您的 ViewScore 类中,只需添加 Select&lt;TSource, TResult&gt; 语句:

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

      【讨论】:

      • 名称s在当前上下文中不存在
      猜你喜欢
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多