linq结果集转换成datatable
时间:2010-08-17 来源:山村果园
扩展方法:
public static class Ext { public static DataTable ConvertToTable(this IEnumerable<ListEntity> obj) { if (obj == null) return null; var ret = new DataTable(); ret.Columns.Add("id", typeof(ListEntity).GetField("id").FieldType); ret.Columns.Add("Fmsn", typeof(ListEntity).GetField("Fmsn").FieldType); ret.Columns.Add("State", typeof(ListEntity).GetField("State").FieldType); ret.Columns.Add("CardSerialNum", typeof(ListEntity).GetField("CardSerialNum").FieldType); ret.Columns.Add("CerGetDate", typeof(ListEntity).GetField("CerGetDate").FieldType); ret.Columns.Add("CerEndDate", typeof(ListEntity).GetField("CerEndDate").FieldType); ret.Columns.Add("OrgSn", typeof(ListEntity).GetField("OrgSn").FieldType); ret.Columns.Add("OrgCode", typeof(ListEntity).GetField("OrgCode").FieldType); ret.Columns.Add("OrgName", typeof(ListEntity).GetField("OrgName").FieldType); ret.Columns.Add("FmName", typeof(ListEntity).GetField("FmName").FieldType); foreach (var item in obj) { DataRow r = ret.NewRow(); r["id"] = item.id; r["Fmsn"] = item.Fmsn; r["State"] = item.State; r["CardSerialNum"] = item.CardSerialNum; r["CerGetDate"] = item.CerGetDate; r["CerEndDate"] = item.CerEndDate; r["OrgSn"] = item.OrgSn; r["OrgCode"] = item.OrgCode; r["OrgName"] = item.OrgName; r["FmName"] = item.FmName; ret.Rows.Add(r); } return ret; } }
实体类:
[Serializable] [DataContract] public class ListEntity { [DataMember] public int id { get; set; } [DataMember] public string Fmsn { get; set; } [DataMember] public int State { get; set; } [DataMember] public string CardSerialNum { get; set; } [DataMember] public System.Nullable<System.DateTime> CerGetDate { get; set; } [DataMember] public System.Nullable<System.DateTime> CerEndDate { get; set; } [DataMember] public string OrgSn { get; set; } [DataMember] public string OrgCode { get; set; } [DataMember] public string OrgName { get; set; } [DataMember] public string FmName { get; set; } }
相关阅读 更多 +