LINQ Map of one type to another

I need to find the difference between sets. Classes containing sets differ from each other, but have the same field types. To be able to use the Except method to make a difference, I want to map one list to another.

Can I do this with the toList method? if not, is this possible in another way?

List<Class1>.Except(List<Class2> I need to map class2 list to class1 list) 

thanks

+4
source share
3 answers

In LINQ, Select is synonymous with "cards" in other languages. It is called "select" because the word comes from database terminology ... but Select is what you want:

var mappedTypes = myCollection.Select(item => new MappedType(item.Something));

+10
source

If you want to project, you can use the olde Select operator:

 list1.Except(list2.Select(x => ConvertToClass1(x)); 
+1
source
 List<Class1>.Except(List<Class2>.Select(e => new Class1() { Field1 = e.Field1 ... }); 

However, I would advise you to use automapper .

0
source

Source: https://habr.com/ru/post/1333402/


All Articles