我創建winForm應用程序,在該Onbutton點擊我從兩個數據庫的Mysql和Sqlite數據庫收集數據表。 在將Linq查詢轉換爲IEnumerable時,我正在執行Casting錯誤,以便將查詢值提取到DataTable以在DataGrid視圖中進行顯示。鑄造Linq IEnumerable <Datarow>
private void button1_Click(object sender, EventArgs e)
{
var obj = new table1TableAdapter(); //Mysql Table Adapter
var obj2 = new Table1TableAdapter(); // Sqlite Table Adapter
var ds = new DataSet();
ds.Tables.Add(obj.GetData());
ds.Tables.Add(obj2.GetData());
var tab1 = ds.Tables[0];
var tab2 = ds.Tables[1];
var query = from o in tab1.AsEnumerable()
join od in tab2.AsEnumerable()
on o.Field<string>("Name") equals od.Field<string>("Name")
select new
{
Name = o.Field<string>("Name"),
Rollno = od.Field<Int64>("rollno"),
Book = o.Field<string>("Book")
};
var q2 = (IEnumerable<DataRow>)query; //Unable to cast object of type <JoinIterator>
DataTable orderTable = q2.CopyToDataTable();
dataGridView1.DataSource = orderTable;
}
這不是一個DataRow,你不能神奇地把它變成'的DataRow ' – SLaks 2011-06-03 15:15:42