-2
我有表名为Pinfo我想要做的是从数据库中使用Linq查询获取记录并返回它。但我无法做到这一点。这是我到目前为止。我如何使这项工作我怎样才能使数据表为可枚举
List<ProductInfo> getList()
{
List<ProductInfo> pi = new List<ProductInfo>();
using (SqlConnection con = new SqlConnection("server=.\\sqlexpress;database=Project;User Id=sa;Password=1"))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("select * from PInfo", con))
{
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
var query = from c in dt.AsEnumerable()
select new
{
id = c.Field<int>("Id"),
name = c.Field<string>("Name"),
price = c.Field<decimal>("Price")
};
adp.Fill(query);
}
}
return pi;
}
我想你忘了在迭代结果之前运行实际查询 – Greenonion 2014-09-04 13:55:55
该表为空。您不能像使用Linq-To-SQL一样使用ADO.NET。你必须先使用'adp.Fill(dt)'。然后你可以使用'Linq-To-DataTable'('Linq-To-Objects')来查询表格。 – 2014-09-04 14:02:31