select子句中表达式的类型不正确。呼叫中选择'选择'

时间:2016-02-29 06:29:32

标签: c# .net linq datatable

我尝试使用此查询 - > C#linq ......

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个错误...请帮助我:&#39;(

错误1

  

查询正文必须以select子句或group子句结尾

错误2

  

select子句中表达式的类型不正确。在调用&#39;选择&#39;

时,类型推断失败

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();

答案 1 :(得分: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();