2016-02-29 86 views
1

我有一個表:C#氯氟>所有行選擇添加ROW_NUMBER

DataTable store_temp = new DataTable(); 
store_temp.Columns.Add("patn"); 
store_temp.Columns.Add("rf"); 
store_temp.Columns.Add("name"); 
store_temp.Columns.Add("conv"); 
store_temp.Columns.Add("conv_type"); 
store_temp.Columns.Add("recorddate"); 
store_temp.Columns.Add("executiondate"); 

我的C#代碼:

int i = 0; 
var rowsgroups = (from row in store_temp.AsEnumerable().GroupBy(row => 
row.Field<string>("patn")) 
.OrderBy((g => g.OrderByDescending(y => y.Field<string("executiondate")).ThenByDescending(y => 
y.Field<string>("rf")))) 
select new 
{ 
    patn = row.ElementAt(i), 
    rf_num = ++i, 
}).ToArray(); 

我想要的拉姆達李毅華,這相當於:

select patn, rf, 

> ROW_NUMBER()以上(分區由patn爲了通過executiondate,RF) 作爲rf_num,

 name, conv,conv_type, recorddate, executiondate 
     from store_temp2 

但是,lambda語法... VAR rowsgroups剛一一行.. 我想顯示store_temp中的所有行。 我應該怎麼做來修復查詢?

+0

我想只是一個附加列ROW_NUMBER ..並更改SQL以拉姆達李毅華.. – lilly

+0

你需要'蘭巴.. sql'不包含任何組..它只需要order_number爲row_number ..你有沒有想念那裏? – Moumit

+0

看看這裏:http://stackoverflow.com/questions/4827370/how-to-get-row-number-via-linq-using-entity-framework – Rob

回答

1

ROW_NUMBER()以上(分區由patn爲了通過executiondate, rf)

意味着在LINQ中你需要按patn進行分組,然後按分組executiondate, rf,然後用索引Select過載得到組內的行編號,最後用SelectMany將結果平坦化。

有了這樣說,相當於LINQ查詢可能是這樣的:

var result = store_temp.AsEnumerable() 
    .GroupBy(e => e.Field<string>("patn"), (key, elements) => elements 
     .OrderBy(e => e.Field<string>("executiondate")) 
     .ThenBy(e => e.Field<string>("rf")) 
     .Select((e, i) => new 
     { 
      patn = key, 
      rf = e.Field<string>("rf"), 
      rf_num = i + 1, 
      name = e.Field<string>("name"), 
      conv = e.Field<string>("conv"), 
      conv_type = e.Field<string>("conv_type"), 
      recorddate = e.Field<string>("recorddate"), 
      executiondate = e.Field<string>("executiondate") 
     })) 
    .SelectMany(elements => elements) 
    .ToArray(); 
+0

Thx !!!!!!!!!!!!! ..非常感謝你。 – lilly

0

嘗試是這樣的

select new 
{ 
    rowNum = store_temp.Rows.IndexOf(row), 
    patn = row.ElementAt(i), 
    rf_num = ++i, 
}).ToArray(); 
+0

這是一個錯誤.. store_temp.Rows.IndexOf(行) 錯誤'System.Data.DataRowCollection.IndexOf(System.Data.DataRow)'的最佳重載方法匹配有一些無效參數 – lilly

+0

您的代碼是否提供錯誤?變量行應該是一個DataRow,不知道爲什麼你沒有我的變化沒有得到一個錯誤。如果當前代碼正在工作,代碼應該可以工 – jdweng

+0

Error2 ..參數1:無法從'System.Linq.IGrouping <字符串,System.Data.DataRow>'轉換爲'System.Data.DataRow'..:'(.................. – lilly

0

我不認爲你需要的任何groupby按您required sql

 var i=0; 
     var rowsgroups = (from row in store_temp.AsEnumerable() 
          orderby row.Field<string>("executiondate") descending, 
          row.Field<string>("rf") descending 
          select new 
          { 
           patn = row.Field<string>("patn"), 
           rf_num = ++i, 
           name = row.Field<string>("name"), 
           conv = row.Field<string>("conv"), 
           conv_type = row.Field<string>("conv_type"), 
           recorddate = row.Field<string>("recorddate"), 
           executiondate = row.Field<string>("executiondate") 
          }).ToArray(); 
+0

不,我不想..由patn列分區.. ..如何在lambda表達式中使用分區? – lilly