开发者

LINQ Map one type to another

开发者 https://www.devze.com 2023-02-01 19:32 出处:网络
I need to find difference b开发者_运维百科etween 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 ma

I need to find difference b开发者_运维百科etween 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


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));


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

list1.Except(list2.Select(x => ConvertToClass1(x));


List<Class1>.Except(List<Class2>.Select(e => 
                                           new Class1() 
                                                    { 
                                                      Field1 = e.Field1 ...
                                                    });

However, I would advise you to use automapper.

0

精彩评论

暂无评论...
验证码 换一张
取 消