2011-06-03 73 views
0

我創建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; 
} 
+3

這不是一個DataRow,你不能神奇地把它變成'的DataRow ' – SLaks 2011-06-03 15:15:42

回答

6

看看你的代碼,我會說,爲什麼要將它投射到IEnumerable<DataRow>?只需將查詢綁定到您的GridView即可。

dataGridView1.DataSource = query.ToList(); 
+0

謝謝它有幫助 – 2011-06-03 15:37:10

4

這是因爲您返回的查詢對象與DataRow沒有關係。 query將成爲IEnumerable < SomeAnonymousType>。預計如何轉換爲DataRow

你需要改變你的聲明作出的DataRow:

select new DataRow(/* Whatever Params */) { /* More Params */ }; 

然後,它本身的IEnumerable<DataRow>,不需要鑄造。

1

一件事,你不會是能夠轉換爲一個IEnumerable因爲你查詢本身不產生數據行

select new 
{ 
    Name = o.Field<string>("Name"), 
    Rollno = od.Field<Int64>("rollno"), 
    Book = o.Field<string>("Book") 
}; 

正在創建一個匿名類型。

你將不得不首先將它改變爲DataRow,然後將其轉換爲IEnumerable。

3

由於您的查詢正在創建IEnumerable,因此您無法將其轉換爲DataRow。我也不會建議使用選擇新的DataRow(/ * Whatever Params /){/ More Params * /};因爲這不會是一個真正的DataRow對象,並且是不好的做法。

我會這樣處理它。即使這是一個小項目,Button_Click處理程序中也不應該有太多的代碼。

首先,創建一個容器對象,稱之爲DTO或ViewModel。我建議後者。

public class BookViewModel 
{ 
    public string Name { get; set; } 

    public Int64 Rollno { get; set; } 

    public string Book { get; set; } 
} 

接下來,創建一個新的類,將執行您的SQL查詢。這會將您的數據訪問邏輯與表單邏輯分開。

public class BookService 
{ 
    public IList<BookViewModel> GetBookViewModel() 
    { 
     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 BookViewModel 
        { 
         Name = o.Field<string>("Name"), 
         Rollno = od.Field<Int64>("rollno"), 
         Book = o.Field<string>("Book") 
        }; 

     return query.ToList(); 
    } 

} 

最後,將列表綁定到您的顯示器。

private void button1_Click(object sender, EventArgs e) 
    { 
     BookService bookService = new BookService(); 
     dataGridView1.DataSource = bookService.GetBookViewModel(); 
    } 

現在,當你回去修改你的代碼,你將能夠輕鬆地修改您的顯示邏輯,並具有通過所有的混雜代碼讀出。

例子:

private void button1_Click(object sender, EventArgs e) 
     { 
      BookService bookService = new BookService(); 
      IList<BookViewModel> books = bookService.GetBookViewModel(); 
      if (books.Count == 0) 
      { 
         Label1.Text = "Sorry no books were found"; 
      } 
      dataGridView1.DataSource = books; 
     } 
0

我用下面的語句,這對我的作品

UC070_WizardStepFilesDataSet.AllDossierDetailResultsRow[] searchrows = 
         (from a in _wizardStepPreviewDataSet.AllDossierDetailResults 
         where a.WingsSetNbr == row.WingsSetNbr && !a.BookCode.StartsWith("V") 
         select a).ToArray<UC070_WizardStepFilesDataSet.AllDossierDetailResultsRow>();