Search code examples
c#.netlinq

check if a item in list, exists in another list with linq


i'm new in c# ,sorry for bad question.

how can i say, if an item in a list,

exists in another list?

l know it can be done using linq functions, but I don't know which method can help.

I want something like this:


bool Exists(List<int> list1, List<int> list2)

{
    // if the value of the first index of list1 exists in list2, return true.

}


Solution

  • well

    you can do this

    bool Exists(List<int> list1, List<int> list2)
    {
    var firstIndex=list1.First();
    
    return list2.Any(num=> num == firstIndex);
    }