简体   繁体   中英

Intersection between sets containing different types of variables

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?

Could you not just do

var query = (from d in values
            join p in points
            on d equals p.Z
            select p).ToList();

?

HashSet<double> values = ...;
IEnumerable<SomePoint> points = ...;

var result = points.Where(point => values.Contains(point.Z));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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