Mapping Linq Query results to a DTO class

前端 未结 4 1094
鱼传尺愫
鱼传尺愫 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:08

    First thing, I just need to ask whether you can use Entity Framework 4.1 and POCOs (DbContext) and avoid the need for DTO's altoghther?

    Assuming that the answer is no, that must be because you are not pulling back all of the fields, or you are somehow altering the "shape" of the data.

    In that case, you could change your LINQ query to look something like this:

    from t in table
    where ...
    select new DTOA()
    {
      TheDtoProperty = theTableProperty,
      AndSoOn = AndSoOn
    };
    

    The benefit of doing it this way: If you turn on SQL Profiler, you should see that only the columns that you request make it into the actual SQL query. If you query it all first and then pull the values, all of the columns will be pulled down the wire.

提交回复
热议问题