Translate a List to List

2019-04-14 03:13发布

问题:

Understood the concept of translate. Used it in converting a DataModel Type to DTO type for presentation layer like this and worked fine.

objTypeB = objTypeA.TranslateTo<clsTypeB>();

Discrepancy between TypeA and TypeB was just the datatype of few properties and I converted them in the Property Set method.

But in the above implementation if the source is List<TypeA>, I have loop through each to translate to TypeB and add it another List<TypeB> instance. Is it possible to do something like this instead:

Assume resultListA is a List<clsTypeA>

var resultListB = resultListA.TranslateTo<List<clsTypeB>>();

I tried and does not seem to convert. I get a empty resultListB. Any easy approach to this??

回答1:

This should do it:

var resultListB = resultListA.ConvertAll(x => x.TranslateTo<clsTypeB>());