简体   繁体   中英

Determine if a List contains elements from another List

I have an object model MyObject that contains a list of long called ObjectList . I have another list called TestList that also contains longs and I want to determine if TheObject.ObjectList contains any elements that are in TestList .

I'm trying with something like this but it's not giving Count as an option.

if (TheObject.ObjectList.Any(TestList).Count() > 0) {...}

How should I rewrite this? Thanks for your suggestions.

Use Intersect :

TheObject.ObjectList.Intersect(TestList).Any()

Produces the set intersection of two sequences by using the default equality comparer to compare values.

Note: There are also Except and Union set opeartions.

 if ( TheObject.ObjectList.Intersect(TestList).Any() ) 
 { 
   ... 
 }

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