Translate a List<TypeA> to List<TypeB>

这一生的挚爱 提交于 2019-12-21 21:25:05

问题


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


来源:https://stackoverflow.com/questions/13315579/translate-a-listtypea-to-listtypeb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!