Linq查询 - 选择性字段为DataTable?

时间:2011-05-29 10:06:28

标签: c#

我想从linq查询创建一个数据表。 以下代码选择所有归档,但我需要选择少量文件,如“door_no”,“street_name”,“city_name”,“post_code”,“出生日期”,并需要将其创建为Datatable。

var Results = from SelRow in MyDtb1.AsEnumerable() where SelRow.Field("door_no") != null select SelRow;
DataTable ChkResult = Results.CopyToDataTable();

任何更好的想法

1 个答案:

答案 0 :(得分:2)

您需要创建一个这样的匿名类型:

var Results = from SelRow in MyDtb1.AsEnumerable()
              where !SelRow.IsNull("door_no")
              select new
              {
                  DoorNumber = SelRow["door_no"],
                  CityName = SelRow["city_name"],
                  ZipCode = SelRow["post_code"]
              };