linq to sql 查询出来的结果 转换成dataset datatable

2024年12月05日 02:24
有1个网友回答
网友(1):

最笨的办法:查询出结果后实例化dataset跟datatable然后把数据复制过去

dt.Columns.Add("UserID", typeof (long));
dt.Columns.Add("UserName", typeof (string));
dt.Columns.Add("UserQuestion", typeof (string));
dt.Columns.Add("UserAnswer", typeof (string));
dt.Columns.Add("UserEmail", typeof (string));
dt.Columns.Add("TrueName", typeof (string));
dt.Columns.Add("UserSex", typeof (string));
dt.Columns.Add("UserBirthday", typeof (string));
dt.Columns.Add("IDCardNumber", typeof (string));
dt.Columns.Add("ProvinceID", typeof (long));
dt.Columns.Add("CityID", typeof (long));
dt.Columns.Add("DistrictID", typeof (long));
dt.Columns.Add("ZipCode", typeof (string));
dt.Columns.Add("LastLoginIP", typeof (string));
dt.Columns.Add("DateUpdated", typeof (DateTime));
dt.Columns.Add("DateCreated", typeof (DateTime));
DataRow dr = null;
ds.Tables.Add(dt);
IEnumerable userInfo = null;
try
{
userInfo = UserOption.U_User_GetUserInfoByID(userID);
foreach (U_User_GetUserInfoByIDResult info in userInfo)
{
dr = dt.NewRow();
dr["UserID"] = info.UserID;
dr["UserName"] = info.UserName;
dr["UserQuestion"] = info.UserQuestion;
dr["UserAnswer"] = info.UserAnswer;
dr["UserEmail"] = info.UserEmail;
dr["TrueName"] = info.TrueName;
dr["UserSex"] = info.UserSex;
dr["UserBirthday"] = info.UserBirthday;
dr["IDCardNumber"] = info.IDCardNumber;
dr["ProvinceID"] = Convert.ToInt64(info.ProvinceID);
dr["CityID"] = Convert.ToInt64(info.CityID);
dr["DistrictID"] = Convert.ToInt64(info.DistrictID);
dr["ZipCode"] = info.ZipCode;
dr["LastLoginIP"] = info.LastLoginIP;
dr["DateUpdated"] = Convert.ToDateTime(info.DateUpdated);
dr["DateCreated"] = Convert.ToDateTime(info.DateCreated);
ds.Tables[0].Rows.Add(dr);
}
}
catch (Exception ex)
{
throw ex;
}
return ds;