这是我的代码
var s = from p in db.tblTotalFees
where p.Class == ClassDropDownList.SelectedItem.Value && p.StudentID == Convert.ToInt32(StudentNameDropDownList.SelectedValue)
select p;
DataTable dt = new DataTable();
如何将p转换为数据表并将其存储在dt ???
中答案 0 :(得分:1)
您可以使用MSDN How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow中描述的CopyToDataTable()
扩展方法。用法:
var selectedClass = ClassDropDownList.SelectedItem.Value;
int id = Convert.ToInt32(StudentNameDropDownList.SelectedValue);
var query = from p in db.tblTotalFees
where p.Class == && selectedClass
p.StudentID == id
select p;
DataTable dt = query.CopyToDataTable(); // here
或者您可以使用纯ADO.NET - 使用SqlDataAdapter创建SqlCommand和填充表。