-2

I have two lists of string. I want to compare each elements in one list with another and if at least one of them match then do some processing else dont do anything.

I dont know how to do. I do have the following lists and the code I used was SequenceEqual but my lead said its wrong as it just compares if its equal or not and does nothing. I couldn't disagree and I want to achieve my intended functionality I mentioned above. Please help. As you seem, order doesn't matter, here 123 is in both list but in different order, so it matches and hence do some processing as per my requirement.

List<string> list1 = new List<string> () { "123", "234" };

List<string> list2 = new List<string> () { "333", "234" , "123"};
Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • 1
    You could use List.Intersect. If the resulting list is empty then no match – Steve Jan 11 '19 at 13:27
  • `list1.Any(x=> list2.Any(y=>x==y))` – sujith karivelil Jan 11 '19 at 13:27
  • @Steve: I actually have both list as string array and I convert to list :p lol. I dont know but for fantasy lol. Idont know how to compare...my lead advised me to use index of compare method using array, I dont know how to do it *Feels angel* – Jasmine Jan 11 '19 at 13:29
  • Possible duplicate of [Any Intersection in Two Collections](https://stackoverflow.com/questions/10627419/any-intersection-in-two-collections) – nosale Jan 11 '19 at 13:30

5 Answers5

4

You can use the Any method for this :

var matchfound = list1.Any(x=> list2.Contains(x));

Now you can do conditional block on the matchFound if it returns true you can process what ever is required.

if you want to do case insentitive comparison then you will need to use String.Equals and can specify if case does not matter for comaparing those.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
3

You can use Intersect to find common elements:

var intersecting = list1.Intersect(list2);

If you just want to know if there are common elements append .Any():

bool atLeastOneCommonElement = intersecting.Any();

If you want to process them:

foreach(var commonElement in intersecting)
{
    // do something ...
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I just want to set a status if there are at least one matching in those two list. This doesn’t throw bull exceptions if one list is empty? Also does it depend on order of items or not? – Jasmine Jan 11 '19 at 21:13
  • No exception and order doesn't matter. – Tim Schmelter Jan 11 '19 at 21:22
1

You could check with Intersect and Any

var matchFound = list1.Intersect(list2).Any();

For example,

List<string> list1 = new List<string>{ "123", "234" };
List<string> list2 = new List<string>{ "333", "234" , "123"};
var result = list1.Intersect(list2).Any();

Output True

List<string> list3 = new List<string>{"5656","8989"};
result = list1.Intersect(list3).Any();

Output False

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
1

You need to take all those item that are matches from both list and then do code if match found like

foreach (var item in list1.Where(x => list2.Contains(x)))
{
    //do some processing here        
    Console.WriteLine($"Match found: {item}");        
}

In above code foreach iterate when item present in both list.

Output:

enter image description here

er-sho
  • 9,581
  • 2
  • 13
  • 26
0

Use LINQ to find the matches; and then check the resulting array size as follows:

var intersect = list1.Where(el1=>list2.Any(el2=>el2==el1));
var isMatch = intersect.Count > 0;
vahdet
  • 6,357
  • 9
  • 51
  • 106