7

I need to find difference between to sets. Classes ,comprising the sets, are different but have same type of fields. To be able to use Except method ,to take the difference, i want to map one list to another.

Can i do this using toList method? if not, Is it possible in another way?

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

Thanks

mehmet6parmak
  • 4,777
  • 16
  • 50
  • 71

3 Answers3

15

In LINQ, Select is synonymous with "map" 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));

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
1

If you want a projection you can use ye olde Select operator:

list1.Except(list2.Select(x => ConvertToClass1(x));
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
0
List<Class1>.Except(List<Class2>.Select(e => 
                                           new Class1() 
                                                    { 
                                                      Field1 = e.Field1 ...
                                                    });

However, I would advise you to use automapper.

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Femaref
  • 60,705
  • 7
  • 138
  • 176