Transfer / Calm property in a generic method?

I am trying to port some code that I wrote to a more general method. Although the method is longer, I have a problem:

public static void Test() { MyObjectType[] list1 = ListMyObjectTypeMethod1(); MyObjectType[] list2 = ListMyObjectTypeMethod2(); List<MyObjectType> linqAblelist1 = new List<MyObjectType>(list1); List<MyObjectType> linqAblelist2 = new List<MyObjectType>(list2); IEnumerable<MyObjectType> toBeAdded = linqAblelist1.Where(x => linqAblelist2.All(y => y.Property1 != x.Property1)); IEnumerable<MyObjectType> toBeDeleted = linqAblelist2.Where(a => linqAblelist1.All(b => b.Property1 != a.Property1)); } 

And I'm trying to pass a generic type to MyObjectType, but where do I have [How to set a property here?], How do I specify what is in the method parameter?

 public static void Test<T>(T[] x, T[] y) { List<T> list1 = new List<T>(x); List<T> list2 = new List<T>(y); IEnumerable<T> toBeAdded = list1.Where(x => list2.All(y => y.[How To Set Property Here?] != x.[How To Set Property Here?])); IEnumerable<T> toBeDeleted = list2.Where(a => list1.All(b => b.[How To Set Property Here?])); != a.[How To Set Property Here?]));)); } 
+7
generics c # func
source share
4 answers

Go to property selection as 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); 
+9
source share

The best option is to introduce a general type constraint that ensures that T either inherits from a particular class or implements an interface. In either case, the class or interface must declare Property1 . For example. eg:

 public static void Test<T>(T[] x, T[] y) where T : IHasProperty1 { โ€ฆ } 
+1
source share

You need to impose some restrictions on your generic type.

 public static void Test<T>(T[] x, T[] y) where T : <SomeInterface> { List<T> list1 = new List<T>(x); List<T> list2 = new List<T>(y); IEnumerable<T> toBeAdded = list1.Where(x => list2.All(y => y.PropertyName != x.PropertyName)); IEnumerable<T> toBeDeleted = list2.Where(a => list1.All(b => b.PropertyName)); != a.PropertyName));)); } 
0
source share

You can add a general constraint that ensures that T will have the properties you expect. Something like:

 public static void Test<T>(T[] x, T[] y) where T : MyObjectType { List<T> list1 = new List<T>(x); List<T> list2 = new List<T>(y); IEnumerable<T> toBeAdded = list1.Where(x => list2.All(y => y.Property1 != x.Property1 )); IEnumerable<T> toBeDeleted = list2.Where(a => list1.All(b => b.Property1 )); != a.[How To Set Property Here?]));)); } 
0
source share

All Articles