2016-02-29 66 views
0

我嘗試使用此查詢 - > C#LINQ ...select子句中表達式的類型不正確。類型推斷呼叫失敗 '選擇'

select patn, rf, row_number() over(partition by patn order by 
executiondate,rf) as rf_num, name, conv,conv_type, recorddate, 
executiondate from store_temp2 

我的C#代碼:

 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"); 
 var rowsgroups = from row in store_temp.AsEnumerable().GroupBy(row =>row.Field<string>("executiondate")) 
         .OrderBy((g=> g.OrderByDescending(y=>y.Field<string>("executiondate")).ThenByDescending(y=> y.Field<string>("rf")))) 
         .Select((n,i) => 
          new { 
          patn = n.ElementAt(0).ToString(), 
          rf = n.ElementAt(1).ToString(), 
          rf_num = i+1, 
          name = n.ElementAt(2).ToString() , 
          conv = n.ElementAt(3).ToString(), 
          conv_type = n.ElementAt(4).ToString(), 
          recorddate = n.ElementAt(5).ToString(), 
          executiondate = n.ElementAt(6).ToString() 

         }).ToArray(); 

它有2個錯誤...請幫助我:'(

錯誤1

查詢體必須與選擇子句或一組子句

錯誤2

選擇子句中的類型的表達的是不正確結束。在「選擇」調用中,類型推斷失敗

+0

首先刪除' –

回答

1

問題是,您正在混合Linq語法和lambda語法不正確。嘗試類似這樣..

int i=1; 
var rowsgroups = (from row in store_temp.AsEnumerable().GroupBy(row =>row.Field<string>("executiondate")) 
       .OrderBy((g=> g.OrderByDescending(y=>y.Field<string>("executiondate")).ThenByDescending(y=> y.Field<string>("rf")))) 
       select 
        new { 
        patn = row.ElementAt(0).ToString(), 
        rf = row.ElementAt(1).ToString(), 
        rf_num = i++, 
        name = row.ElementAt(2).ToString() , 
        conv = row.ElementAt(3).ToString(), 
        conv_type = row.ElementAt(4).ToString(), 
        recorddate = row.ElementAt(5).ToString(), 
        executiondate = row.ElementAt(6).ToString() 

       }).ToArray(); 
0

請檢查this。我認爲MSDN上的同樣的問題。

var rowsgroups = from row in store_temp.GroupBy(row =>row.Field<string>("executiondate")) 
         .OrderBy((g=> g.OrderByDescending(y=>y.Field<string>("executiondate")).ThenByDescending(y=> y.Field<string>("rf")))) 
         .AsEnumerable()//and please try .AsEnumerable() here 
         .Select((n,index) => 
         new { 
          index, 
          patn = n.ElementAt(0).ToString(), 
          rf = n.ElementAt(1).ToString(), 
          rf_num = i+1, 
          name = n.ElementAt(2).ToString() , 
          conv = n.ElementAt(3).ToString(), 
          conv_type = n.ElementAt(4).ToString(), 
          recorddate = n.ElementAt(5).ToString(), 
          executiondate = n.ElementAt(6).ToString() 

         }).ToArray();