2011-10-06 9 views
0

我一直在使用ORM的這麼久了,我似乎已經忘記了我的大部分基本的數據處理能力在DOTNET :(是否有可能通過數據集中的相關數據表對數據表中的數據進行排序?

是否有可能做這樣的事情?

 DataSet ds = new DataSet(); 
     var compiledConnection = new SqlConnection(cDbConnectionString); 
     SqlDataAdapter daChart = new SqlDataAdapter("select * from Chart", compiledConnection); 
     daChart.Fill(ds, "chart"); 

     if (ds.Tables["chart"].Rows.Count > 0) 
     { 
      var sourceConnection = new SqlConnection(sourceDbConnectionString); 
      SqlDataAdapter daSource = new SqlDataAdapter("select * from source", sourceConnection); 
      daSource.Fill(ds, "source"); 

      DataRelation chart_source = new DataRelation("dr", ds.Tables["chart"].Columns["intItemId"], 
        ds.Tables["source"].Columns["intRowId"], false); 
      ds.Relations.Add(chart_source); 
     } 

然後使用表「圖表」中的一列來排列數據表中「源」表中的數據?

(在任何人詢問之前,這兩個表位於不同站點上的SqlServer的分離實例中,作爲一個表的數據不是一個簡單的任務,因此這種方法)

乾杯, 馬特

回答

0

感謝您的建議,但我發現,你可以做到這一點LINQ相當容易:

  DataTable source = ds.Tables["source"]; 
      DataTable chart = ds.Tables["chart"]; 

      var joinedTable = 
       from s in source.AsEnumerable() 
       join c in chart.AsEnumerable() 
       on s.Field<Int64>("intRowId") equals 
        c.Field<Int64>("intItemId") 
       select new 
       { 
        intRowId = s.Field<Int64>("intRowID"), 
        strTitle = s.Field<string>("strTitle"), 
        intWeight = c.Field<Int64>("intWeight") 
       }; 

      var sortedTable = from j in joinedTable 
           orderby j.intWeight descending 
           select j; 
0

這只是創建一個外鍵的等價物。你似乎想要一個INNER JOIN的等價物。

除了創建的關係,就需要增加一個的所有列到其他,循環填補行和GetParentRows。 MS有一些很好的起點代碼:

http://support.microsoft.com/kb/326080

編輯。你也可以做一個版本的SQL,通過創建一個鏈接的服務器,並使用4個部分名稱[服務器]。[數據庫]。[所有者] [表]

相關問題