select 子句中的表达式类型不正确.类型推断在调用“选择”时失败

本文关键字:类型 调用 选择 失败 子句 表达式 select 不正确 | 更新日期: 2023-09-27 18:36:43

我尝试使用此查询 -> 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 个错误...请帮助我:'(

错误 1

查询正文必须以选择子句或组子句结尾

错误 2

select 子句中的表达式类型不正确。 类型推断在调用"选择"时失败

select 子句中的表达式类型不正确.类型推断在调用“选择”时失败

问题是,您错误地混合了 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();

请检查这个。我认为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();