I have the following function
public static class ListUtils
{
public static bool ListsHaveCommonality<T>(List<T> listOne, List<T> listTwo, Func<T, string> selectorOne, Func<T, string> selectorTwo)
{
return listOne.Select(selectorOne).Intersect(listTwo.Select(selectorTwo)).Any();
}
}
Then a test to check it works
//Arrange
List<Alias> aliases = new List<Alias>();
Alias a1 = new Alias { alias = "[email protected]" };
Alias a2 = new Alias { alias = "[email protected]" };
Alias a3 = new Alias { alias = "[email protected]" };
aliases.Add(a1);
aliases.Add(a2);
aliases.Add(a3);
List<string> chosenAliases = new List<string>
{
"[email protected]",
"[email protected]",
};
//Act
bool hasCommonality = ListUtils.ListsHaveCommonality(aliases, chosenAliases, (Alias a) => a.alias, (string s) => s);
//Assert
Assert.IsTrue(hasCommonality);
I get the following error
The type arguments for method System.Func, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
ListsHaveCommonality
assumes that both lists have the same type parameterT
. – Yacoub Massadpublic static bool ListsHaveCommonality<T, T2>(List<T> listOne, List<T2> listTwo, Func<T, string> selectorOne, Func<T2, string> selectorTwo)
– user2436996