2013-11-14 119 views
0

通過試圖避免許多foreach() - > if()例程,我嘗試了一個lambda概念來搜索一堆DataTable。我沒有錯誤,直到我調試我的代碼,看看它不工作,因爲它不允許問我的datarow列索引...有沒有辦法使這個工作,而不是使用IndexOf()?如何通過列值獲取特定DataRow的索引

static Entity.Produkt ProduktConstructor(DataRow dr) 
    { 
     Entity.Produkt p = new Entity.Produkt(); 
     DataTable dt = Entity.KbMirror.mirror.Tables["Produkt"]; 

     p.id = Guid.Parse(dr[0].ToString()); 
     p.name = dr[1].ToString(); 
     byte[] ba = dt.Rows[dt.IndexOf(dt.Select().Where(r => r[0].ToString() == p.id.ToString()))]["ProduktLogo"]; 
     p.logo = Converter.ImageConverter.BA2Image(ba); 
     foreach (DataRow pvdr in Entity.KbMirror.mirror.Tables["Produkt_Version"].Rows) 
      if (Guid.Parse(pvdr[1].ToString()) == p.id) 
       p.version.Add(VersionConstructor(Guid.Parse(pvdr[2].ToString()), Guid.Parse(pvdr[0].ToString()))); 
     return p; 
    } 
    static Entity.Version VersionConstructor(Guid vid, Guid pvid) 
    { 
     Entity.Version version = new Entity.Version(); 
     DataTable dt = Entity.KbMirror.mirror.Tables["Version"]; 

     version.id = vid; 
     version.name = dt.Rows[dt.IndexOf(dt.Select().Where(r =>r[0].ToString() == vid.ToString()))][1].ToString(); 
     foreach (DataRow cvdr in Entity.KbMirror.mirror.Tables["Customer_ProduktVersion"].Rows) 
      if (Guid.Parse(cvdr[2].ToString()) == pvid) 
       version.customerCollection.Add(CustomerConstructor(Guid.Parse(cvdr[1].ToString()))); 
     return version; 
    } 

編輯:

 byte[] ba = dt.Rows[dt.IndexOf(dt.Select().Where(r => r[0].ToString() == p.id.ToString()))]["ProduktLogo"]; 
+0

我建議使用 'LINQ到數據集'。 –

回答

0

您是否嘗試過通過整個表,而不是僅僅該行:當我用 「的IndexOf()」 像這樣

錯誤occures?一旦你傳遞了整個表格,那麼你可以引用表格的那一行。

另外:http://msdn.microsoft.com/en-us/library/bb552415%28v=vs.110%29.aspx

 // Fill the DataSet. 
    DataSet ds = new DataSet(); 
    ds.Locale = CultureInfo.InvariantCulture; 
    FillDataSet(ds); 

    DataTable products = ds.Tables["Product"]; 

    IEnumerable<DataRow> query = 
     from product in products.AsEnumerable() 
     select product; 

    Console.WriteLine("Product Names:"); 
    foreach (DataRow p in query) 
    { 
     Console.WriteLine(p.Field<string>("Name")); 
    } 
+0

這不是真的我在找什麼。實際上,我只想通過特殊列中的任何值查詢某行的索引的數據表。 –

相關問題