简体   繁体   English

在C#中编写LINQ以过滤通用PointF列表中的max x / y

[英]Write a LINQ in C# to filter max x/y in generic PointF list

If I have a list contains PointF like: 如果我有一个包含PointF的列表:

[10.5, 5.5]
[8.5, 4.5]
[10.5, 6.5]
[5.5, 10.5]
[10.5, 3.5]

Is it possible to have a LINQ to find the max value of x first and then find the max value of Y from among those max X value to yield the result like: 是否有可能让LINQ首先找到x的最大值,然后从这些最大X值中找到Y的最大值,以产生如下结果:

[10.5, 6.5] 
points.OrderBy(i => i.x).ThenBy(i => i.y).Last();

or if you preffer LINQ query syntax: 或者如果您使用LINQ查询语法:

(from point in points
orderby point.x, point.y 
select point).Last();

Following groups by the x-values, takes the highest group and select the highest y value in that group. 按x值跟随组,获取最高组并选择该组中的最高y值。

PointF maxPoint = points
        .GroupBy(p => p.X)
        .OrderByDescending(g => g.Key)
        .Take(1)
        .Select(g => new PointF(g.Key, g.Max(p => p.Y)))
        .First();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM