简体   繁体   中英

How do we compare elements in two lists in a Where Clause (Linq)

class objA
{
    public Rectangle area;    
}

class objB
{
    public Point somepoint;
}


List<objA> listA;
List<objB> listB;

Now, I want to get the elements in listA and listB wherever objA.area.contains(objB.point)

This should do the trick:

result = listA.Select(a=>
new{ 
    Rectangle = a, 
    Points =listB.Where(b=>a.Contains(b))
});

You might consider doing a from from where style query, which is also called a non-equi join:

from rect in listA
from point in listB
where rect.Area.Contains(point.SomePoint)
select new { rect, point }

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