如何返回/如何将数据集转换为C#中的对象列表(List)?
public static DataSet GetComponentData(int CustomerId, int UserId)
{
string connectionString = ConfigurationReader.ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand command = new SqlCommand("GetComponentData", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@CustomerID", SqlDbType.Int).Value = CustomerId;
command.Parameters.Add("@UserId", SqlDbType.Int).Value = UserId;
da.SelectCommand = command;
try
{
da.Fill(ds);
connection.Close();
return ds;
}
catch (Exception ex)
{
string strError = ex.ToString();
return ds = null;
}
finally
{
da = null;
command = null;
connection.Close();
}
}
}
我需要将结果作为列表返回eg.List。我这样做了:
public static string GetGenericListData(string folderName, int CustomerId)
{
//--------------------------------------------------------
// List<Device>lstDevices=new List<Device>();
Devices lstDevices = new Devices();
DataSet ds=new DataSet();
try
{
ds = GetComponentData(CustomerId, UserId);
if(ds.Tables[0].Rows.Count >0)
{
foreach(DataRow dr in ds.Tables[0].Rows)
{
Device _PptDevices=new Device();
_PptDevices.Country = ((dr["Country"]) != null) ? Convert.ToString(dr["Country"]) : "";
_PptDevices.Area = ((dr["Area"]) != null) ? Convert.ToString(dr["Area"]) : "";
_PptDevices.Branch = ((dr["Branch"]) != null) ? Convert.ToString(dr["Branch"]) : "";
_PptDevices.SalesOffice = ((dr["Sales Office"]) != null) ? Convert.ToString(dr["Sales Office"]) : "";
_PptDevices.CustomerNumber = ((dr["Customer Number"]) != null) ? Convert.ToString(dr["Customer Number"
lstDevices.Add(_PptDevices);
_PptDevices = null;
}
}
}
catch
{
}
finally
{
}
string strStatus = "";
if (lstDevices.Count > 0)
{
string FilePath = ConfigurationReader.FileLocation;
XmlSerializer serializer = new XmlSerializer(typeof(Devices));
MemoryStream memStream = new MemoryStream();
serializer.Serialize(memStream, lstDevices);
FileStream file = new FileStream (folderName "\\ComponentData.xml", FileMode.Create, FileAccess.ReadWrite);
memStream.WriteTo(file);
file.Close();
strStatus = "1"; //Success
}
else
{
strStatus = "0"; //Failure
}
return strStatus;
}
这里我要做的是从一个函数获取数据集并使用另一个函数将其转换为列表。有没有直接的方法将数据集转换为列表更容易?或者我们如何将结果集直接分配给列表.. 还有其他简单的解决方案吗?
答案 0 :(得分:1)
让学生成为班级,然后您可以执行以下操作:
dTable = ds.Tables[0];
List<Device> studentList=dTable.AsEnumerable().Select(x => new Device() {
Country =x["column_id"]!=null?x.Field<string>("column_id"):"",
Area = x["Area"]!=null?x.Field<string>("Area"):""
Branch = x["Branch"]!=null?x.Field<string>("Branch"):"",
SalesOffice = x["Sales Office"]!=null?x.Field<string>("Sales Office"):"",
CustomerNumber = x["Customer Number"]!=null?x.Field<string>("Customer Number"):""
}).ToList();
答案 1 :(得分:0)
如果您对DataSet
感兴趣,甚至不需要将其转换为List<>
,您可以直接从阅读器转换它。
var reader = command.ExecuteReader();
var list = reader.Select(r => new Propertyclass() // specify the class name
{
Id = r["id"] is DBNull ? null : r["id"].ToString(),
Name = r["name"] is DBNull ? null : r["name"].ToString()
})
.ToList();