使用 LINQ 从所有子记录中查找字段的 MAX 或 MIN

发布于 2024-11-02 17:27:02 字数 534 浏览 0 评论 0 原文

请查看下面我的简短示例代码,并告诉我是否有更好、更紧凑的方法来编写 LINQ 查询。父类只是学生的姓名和考试成绩数据列表。子类是单个测试分数数据。我想从所有 TestScore 值中找到最差(或最好)的分数,然后确定分数最高的学生。

谢谢!

Dim query = From s In studentList _
            Where s.ScoreList.Select(Function(d) d.TestScore).Min _
                = studentList.SelectMany(Function(g) g.ScoreList).Select(Function(h) h.TestScore).Min _
            Select StudentName = s.Student, _
                   WorstScore = s.ScoreList.Select(Function(g) g.TestScore).Min

Please review my brief example code below and tell me if there is better, tighter way to write the LINQ query. The parent class is simply a student's name and a list of test score data. The child class is a single test score datum. I want to find the worst (or best) score out of all of the TestScore values and then identify the student who had the best score.

Thanks!

Dim query = From s In studentList _
            Where s.ScoreList.Select(Function(d) d.TestScore).Min _
                = studentList.SelectMany(Function(g) g.ScoreList).Select(Function(h) h.TestScore).Min _
            Select StudentName = s.Student, _
                   WorstScore = s.ScoreList.Select(Function(g) g.TestScore).Min

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌能量女王 2024-11-09 17:27:02

我不是 VB 人员,因此可能存在一些语法错误,但我可以建议两项改进:

  • 不要使用 Select 然后 Min,而是使用接受投影的 Min 重载
  • 不要在每个上找到总体最小值迭代;只需执行一次:

    Dim badScore = StudentList.SelectMany(Function(g) g.ScoreList) _
                                .Min(函数(h) h.TestScore)
    
    暗淡查询 = From s In StudentList _
                其中 s.ScoreList.Min(Function(d) d.TestScore) = badScore
                选择学生
    

请注意,我已从查询中删除了匿名类型 - 我们已经知道 worstScore,因此我们实际上不需要将其与查询结果一起使用。请注意,仍然可能有多名学生的成绩最差。

I'm not a VB person so there may be some syntax errors, but I can suggest two improvements:

  • Rather than using Select then Min, use the overload of Min which accepts a projection
  • Don't find the overall minimum on every iteration; just do it once:

    Dim worstScore = studentList.SelectMany(Function(g) g.ScoreList) _
                                .Min(Function(h) h.TestScore)
    
    Dim query = From s In studentList _
                Where s.ScoreList.Min(Function(d) d.TestScore) = worstScore
                Select s.Student
    

Note that I've removed the anonymous type from the query - we know the worstScore already, so we don't really need it in with the query result. Note that there could still be multiple students with that worst score though.

在巴黎塔顶看东京樱花 2024-11-09 17:27:02

如何

Dim query = studentList.OrderBy(Function(d) d.ScoreList.Select(Function(d) d.TestScore).Min).First()

使用 OrderByDescendingMax 来查找最佳分数。

How about

Dim query = studentList.OrderBy(Function(d) d.ScoreList.Select(Function(d) d.TestScore).Min).First()

Using OrderByDescending and Max to find the best score.

薄凉少年不暖心 2024-11-09 17:27:02

如果我是你,我要做的第一件事就是向 StudentScores 添加 MinimumScoreMaximumScore 属性,使 AddScore 保留跟踪迄今为止您所看到的最低和最高分数。现在,对于给定的学生,您可以在 O(1) 时间内找到这些,而不是在 O(n) 时间内

然后您要做的是编写 的实现>MaxByMinBy。我的 VB 讲得不太流利,但是您可以在之前的 MaxBy -in-c/4888703#4888703">回答并相应地转换为VB。

最后,完成所有这些后,您可以说

studentWithBestScore = studentList.MaxBy(s => s.MaximumScore);
var bestScore = studentWithBestScore.MaximumScore;

(同样,您必须将其转换为 VB。)

The first thing I would do if I were you is add a MinimumScore and MaximumScore property to StudentScores make AddScore keep track of the minimum and maximum score that you've seen so far. Now for a given student you can find these in O(1) time instead of in O(n) time

Then what you want to do is write an implementation of MaxBy and MinBy. I don't speak VB fluently, but you can find an implementation of MaxBy in C# in a previous answer and convert to VB accordingly.

Finally, having done all this, you can say

studentWithBestScore = studentList.MaxBy(s => s.MaximumScore);
var bestScore = studentWithBestScore.MaximumScore;

(Again, you'll have to convert this to VB.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文