C# 如何将数据选择到列表中<;T>;而不是数据表?

C# 如何将数据选择到列表中<;T>;而不是数据表?,c#,c#-4.0,C#,C# 4.0,这就是我目前从数据库中选择数据的方式: public DataTable GetData() { DataTable table = new DataTable("Table"); using (SqlConnection connection = new SqlConnection("Connection string")) { SqlCommand command = new SqlCommand(); command.Connecti

这就是我目前从数据库中选择数据的方式:

public DataTable GetData()
{
    DataTable table = new DataTable("Table");
    using (SqlConnection connection = new SqlConnection("Connection string"))
    {
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = System.Data.CommandType.Text;
        command.CommandText = "query string";

        connection.Open();
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        adapter.Fill(table);
    }
    return table;
}
但它返回DataTable,我想选择List而不是DataTable。像这样:

public List<MyClass> GetData()
{
    DataTable table = new DataTable("Table");
    using (SqlConnection connection = new SqlConnection("Connection string"))
    {
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = System.Data.CommandType.Text;
        command.CommandText = "query string";

        connection.Open();
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        adapter.Fill(table);
    }
    ...
    return [List of MyClass];
}
public List GetData()
{
数据表=新数据表(“表”);
使用(SqlConnection连接=新的SqlConnection(“连接字符串”))
{
SqlCommand=newsqlcommand();
command.Connection=连接;
command.CommandType=System.Data.CommandType.Text;
command.CommandText=“查询字符串”;
connection.Open();
SqlDataAdapter=新SqlDataAdapter(命令);
适配器。填充(表格);
}
...
return[MyClass列表];
}
我该怎么做


谢谢

如果您使用的是ADO.NET方法,您将得到一个数据表,您可以将其转换为列表或IEnumberable

或者,如果您不想深入研究LINQtoSQL或实体框架,您可以研究像nHibernate这样的ORM工具,或者使用LINQtoSQL。在大多数情况下,用
IDataReader来实现你的结果是不值得的。

试试这个代码

public List<MyClass> GetData()
{
    DataTable table = new DataTable("Table");
    using (SqlConnection connection = new SqlConnection("Connection string"))
    {
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = System.Data.CommandType.Text;
        command.CommandText = "query string";
        connection.Open();
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        adapter.Fill(table);
        List<MyClass> list=new List<MyClass>();
        foreach(DataRow row in table)
        {
            MyClass instance = new MyClass();
            instance.ID = row["ID"];
            //similarly for rest of the properties

            list.Add(instance);
        }

    }

    return list;
}
public List GetData()
{
数据表=新数据表(“表”);
使用(SqlConnection连接=新的SqlConnection(“连接字符串”))
{
SqlCommand=newsqlcommand();
command.Connection=连接;
command.CommandType=System.Data.CommandType.Text;
command.CommandText=“查询字符串”;
connection.Open();
SqlDataAdapter=新SqlDataAdapter(命令);
适配器。填充(表格);
列表=新列表();
foreach(表中的数据行)
{
MyClass实例=新建MyClass();
instance.ID=行[“ID”];
//对于其余的属性也是如此
列表。添加(实例);
}
}
退货清单;
}

如果要使用
DataRowCollection
填充自定义对象列表,可以使用LINQ和对象初始值设定项:

var lst = table.AsEnumerable().Select(r =>
    new MyObject
    {
        FirstProperty = r.Field<int>("FirstProperty"),
        OtherProperty = r.Field<string>("OtherProperty")
    }).ToList(); 
var lst=table.AsEnumerable().Select(r=>
新对象
{
FirstProperty=r.字段(“FirstProperty”),
OtherProperty=r.字段(“OtherProperty”)
}).ToList();

您确定要使用低级ADO.NET调用而不是LINQ到SQL或实体框架吗?我不确定。但是我认为低级的ADO.NET会更快。是的,ADO.NET会更快,但是速度的提高会对你的环境产生影响?更快的代码总是更好。不,它真的不是。可维护和可读的代码通常更好——当您发现瓶颈真正在哪里时,您可以以简单性为代价挤出微小的优化。如果你追求的是绝对最快的代码,为什么不用手工修改的汇编程序来编写呢?简洁的网络是一个很有用的链接。不幸的是,我没有投票权。实际上,再也没有必要编写这样的代码了。