Mapping Linq Query results to a DTO class

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

    I would change your DTO from List to IEnumerable and than do everything in a LINQ query.

    var query = 
        from ent in TableA
        select new TableA_DTO
        {
            TableAProperty = a.Property,
            TableB_records = 
                from b in TableB
                where ent.Key == b.Key
                select new TableB_DTO
                {
                    TableBProperty = b.Property,
                    TableC_records =
                        from c in TableC
                        where b.Key == c.Key
                        select new TableC_DTO
                        {
                            TableCProperty = c.Property
                        }
                }
        };
    

提交回复
热议问题