I’m trying to construct a lambda expression that will match elements of one array with a second. Below is a simplified version of this query:
class Program
{
static void Main(string[] args)
{
string[] listOne = new string[] { "test1", "test2", "test3" };
MyClass[] listTwo = new MyClass[] { new MyClass("test1") };
string[] newVals = listOne.Where(p => listTwo.Select(e => e.Name).Equals(p)).ToArray();
//string[] newVals2 = listOne.Intersect(listTwo.Select(t => t.Name)).ToArray();
}
class MyClass
{
public MyClass(string name)
{
Name = name;
}
public string Name {get; set;}
}
}
I would expect newVals to return an array of 1 value, but it’s empty. I realise that uncommenting myVals2 would achieve the same result, but the lists of classes differ more fundamentally than shown.
You are using
Equalsbut you should useContains. You are checking wheterIEnumerable<>is equal top, but you want to check ifIEnumerable<>containsp, so replace:with