详见代码static void Main(string[] args){ List<ScoreModel> scoreList = new List<ScoreModel>(){ new ScoreModel(){Id = 1, Score = 30}, new ScoreModel(){Id = 2, Score = 20}, new ScoreModel(){Id = 3, Score = 30}, new ScoreModel(){Id = 4, Score = 20}, new ScoreModel(){Id = 5, Score = 15}, new ScoreModel(){Id = 6, Score = 10}, new ScoreModel(){Id = 7, Score = 30}, new ScoreModel(){Id = 8, Score = 40}, }; int userScore = 33; var result = from i in scoreList where [how to?] select i; //获取i.score小于userScore的最大值 //即所有小于33的列表中取出最大值30的3个元素 //期望结果:包含三个元素id=1,3,7的List<ScoreModel>}class ScoreModel{ public int Id { get; set; } public int Score { get; set; }}
1 回答
慕哥6287543
TA贡献1831条经验 获得超10个赞
var result = (from x in scoreList where x.Score < userScore group x by x.Score into g orderby g.Key descending select g).First(); //或者用
var result2 = scoreList.Where(x => x.Score < userScore)
.GroupBy(x => x.Score)
.OrderByDescending(g => g.Key)
.First();
添加回答
举报
0/150
提交
取消