包含不同类型变量的集合之间的相交(Intersection between sets containing different types of variables)

编程入门 行业动态 更新时间:2024-10-22 17:20:19
包含不同类型变量的集合之间的相交(Intersection between sets containing different types of variables)

假设我们有两个集合:

List<double> values List<SomePoint> points

其中SomePoint是包含三个坐标点的类型:

SomePoint { double X; double Y; double Z; }

现在,我想执行这两个集合之间的交集,以找出哪些点中的points的z坐标等于某个元素的values

我创建了这样的东西:

HashSet<double> hash = new HashSet<double>(points.Select(p=>p.Z)); hash.IntersectWith(values); var result = new List<SomePoints>(); foreach(var h in hash) result.Add(points.Find(p => p.Z == h));

但是它不会返回这些具有相同Z值但是不同的X和Y 。 有没有更好的方法来做到这一点?

Let's assume we have two collections:

List<double> values List<SomePoint> points

where SomePoint is a type containing three coordinates of the point:

SomePoint { double X; double Y; double Z; }

Now, I would like to perform the intersection between these two collections to find out for which points in points the z coordinate is eqal to one of the elements of values

I created something like that:

HashSet<double> hash = new HashSet<double>(points.Select(p=>p.Z)); hash.IntersectWith(values); var result = new List<SomePoints>(); foreach(var h in hash) result.Add(points.Find(p => p.Z == h));

But it won't return these points for which there is the same Z value, but different X and Y. Is there any better way to do it?

最满意答案

HashSet<double> values = ...; IEnumerable<SomePoint> points = ...; var result = points.Where(point => values.Contains(point.Z)); HashSet<double> values = ...; IEnumerable<SomePoint> points = ...; var result = points.Where(point => values.Contains(point.Z));

更多推荐

本文发布于:2023-08-06 21:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   不同类型   Intersection   variables   types

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!