Translate a List to List

2019-04-14 03:16发布

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条回答
聊天终结者
2楼-- · 2019-04-14 03:39

This should do it:

var resultListB = resultListA.ConvertAll(x => x.TranslateTo<clsTypeB>());
查看更多
登录 后发表回答