将数据集作为 DTO 返回 class

Returning DataSet as DTO class

我在 WCF 服务中有一个方法,它是 returning DataSet 到客户端。现在我想将该 DataSet 转换为 DTO 并将 return 作为 DTO 转换为 cleint。 谁能建议如何实现这一目标。

如果您的数据-table 包含 IdName 列,并且您的 DTO class 类似于以下内容:

public class DTOClass
{
 public string Id {get;set;}
 public string Name {get;set;}
}

您可以像这样将数据-table 转换为相应的 DTO 对象列表。

IList<DTOClass> items = dataTable.AsEnumerable().Select(row => 
new DTOClass
    {
        Id = row.Field<string>("Id"),
        Name = row.Field<string>("Name")
    }).ToList();