Pass in the selection of the property as a Func<T, TProperty>
:
public static void Test<T, TProperty>(T[] x, T[] y, Func<T, TProperty> propertySelector)
{
List<T> list1 = new List<T>(x);
List<T> list2 = new List<T>(y);
IEnumerable<T> toBeAdded = list1.Where(x => list2.All(y => !propertySelector(y).Equals(propertySelector(x))));
IEnumerable<T> toBeDeleted = list2.Where(a => !list1.All(b => propertySelector(b).Equals(propertySelector(a))));
}
Then you can call it by specifying a lambda expression for propertySelector
:
Test(someArray, someOtherArray, t => t.SomeProperty);