我有这样的功能
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.score1
, s.name
和 s.playdate
而不是全部(通过 select
)?
最佳答案
是的,你可以像这样用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();
关于c# - 是否可以在单个 LINQ 查询中完成所有这些操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33448655/