How to check list A contains any value from list B?

后端 未结 8 688
遇见更好的自我
遇见更好的自我 2021-01-30 08:04

List A:

1, 2, 3, 4

List B:

2, 5

How to check if list A contains any value from list B?

e.g. someth

8条回答
  •  一生所求
    2021-01-30 08:58

    Sorry, if this is irelevant, but will return list with matches using FindAll() in case you need this:

            private bool IsContain(string cont)
        {
            List ListToMatch= new List() {"string1","string2"};
    
            if (ListToMatch.ToArray().Any(cont.Contains))
            {
                return false;
            }
            else
                return true;
        }
    

    And usage:

    List ListToCheck = new List() {"string1","string2","string3","string4"};
    List FinalList = ListToCheck.FindAll(IsContain);
    

    The final list contains only the matched elements string1 and string2 from list to check. Can easy be switched to int List.

提交回复
热议问题