Mapping Linq Query results to a DTO class

前端 未结 4 1095
鱼传尺愫
鱼传尺愫 2020-12-17 21:43

I want to get records from the database using EF and assign the values to a DTO class.Consider the following tables for a Linq query.

TableA,TableB, TableC

F

4条回答
  •  清歌不尽
    2020-12-17 22:03

    I would make a factory method, ie: TableA_DTO CreateDTO(TableAItem item);

    Using this, you could just rewrite your query as:

    IEnumerable = TableA.AsEnumerable().Select(CreateDTO);
    

    This would give you the collection of "DTO" objects directly.

    That being said, if you're using Entity Framework, the EF Code First added in recent versions might be more useful in this case.

提交回复
热议问题