相关疑难解决方法(0)

将Linq查询结果映射到DTO类

我想使用EF从数据库中获取记录,并将值分配给DTO类.请考虑以下表格查看Linq查询.

表A,表B,表C.

对于每个TableA记录,TableB中有多个记录.对于每个TableB记录,TableC中有多个记录.现在我的DTO看起来像这样

public class TableA_DTO
{
    public int tableA_rowid { get; set; }
    //remaining tableA field definitions

    public List<TableB_DTO> TableB_records { get; set; }
}

public class TableB_DTO
{
    public int tableB_rowid { get; set; }
    //remaining tableB  field definitions

    public List<TableC_DTO> TableC_records { get; set; }
}

public class TableC_DTO
{
    public int tableC_rowid { get; set; }
    //remaining tableC field definitions
}
Run Code Online (Sandbox Code Playgroud)

我的linq查询看起来像这样

var qry = from ent in TableA
          select ent;
Run Code Online (Sandbox Code Playgroud)

在我的映射类中,我遍历查询结果中的项,如下所示:

    foreach (var dataitem in …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-entities linq-to-sql

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

linq ×1

linq-to-entities ×1

linq-to-sql ×1