Can you use linq to see if the two IEnumerables of the data contain any common records?

IEnumerable<fishbiscuits> a = GetFishBiscuits(0); IEnumerable<fishbiscuits> b = GetFishBiscuits(1); if ([any of the results in either list match]) { // Do something ie Console.WriteLine("I see both a and b love at least one of the same type of fish biscuit!"); } 

Can linq be used to see if two IEnumerables of data contain any common records?

+4
source share
2 answers

Yes, you can do this using Intersect and Any :

 bool anyCommonEntries = a.Intersect(b).Any(); 
+9
source
 public void Linq50() { int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; var commonNumbers = numbersA.Intersect(numbersB); Console.WriteLine("Common numbers shared by both arrays:"); foreach (var n in commonNumbers) { Console.WriteLine(n); } } 

From 101 Linq Samples - Intersect

Msdn Documentation for Intersect

Extension of Roundup Methods: Intersect, Union, AsNullable, and GroupEvery

+1
source

Source: https://habr.com/ru/post/1342667/


All Articles