【问题标题】:Error received "No best type found for implicitly-typed array"收到错误“未找到隐式类型数组的最佳类型”
【发布时间】:2012-06-28 12:37:41
【问题描述】:

这是我为 jqGrid 创建 Json 响应和定义单元格成员的 new 关键字的代码,我收到以下消息“未找到隐式类型数组的最佳类型”。

var resRows = results.Select(record => 
            new 
            {
                id = record.Reference,
                cell = **new** []
                {
                    record.Reference,
                    record.TradeDate.ToShortDateString(),
                    record.Currency1,
                    record.Currency2,
                    record.Notional.ToString(),
                    record.EffectiveDate.ToShortDateString(),
                    record.Quote.ToString()                        
                }
            }).ToArray();

我在这里做错了什么?

【问题讨论】:

    标签: c# json implicit


    【解决方案1】:

    假设ReferenceCurrency1Currency2是字符串,只要声明为字符串数组即可:

    var resRows = results.Select(record => 
    
        new 
        {
            id = record.Reference,
            cell = new string []
            {
                record.Reference,
                record.TradeDate.ToShortDateString(),
                record.Currency1,
                record.Currency2,
                record.Notional.ToString(),
                record.EffectiveDate.ToShortDateString(),
                record.Quote.ToString()                        
            }
        }).ToArray();
    

    【讨论】:

    • @D Stanley,我做到了,而且成功了!实际上,如果这些不是字符串,我可以使用cell = new object [] {... }
    【解决方案2】:

    如果您为 jqGrid 准备数据(如在您的代码中),您可以定义自己的 jsonReader 并跳过单元格数组 (http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data):

    jsonReader: {
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems: false,
            userdata: "userdata"
        },
    

    然后是这样的:

    var result = new
    {
        total = (int)count / grid.PageSize),
        page = grid.PageIndex,
        records = count,
        rows = results.Select(record => 
                        select new
                        {
                            Reference = record.Reference,
                            TradeDate = record.TradeDate,
                            ..
                         }).ToArray()
    }
    

    【讨论】:

    • 将“选择新”替换为“新”
    【解决方案3】:

    我遇到了同样的问题,发现如果数组中的所有数据项都是相同的类型(例如 String),那么类型就会被推断出来,编译器不会抱怨 new[] .

    【讨论】:

      【解决方案4】:

      如果集合的成员是函数,它仍然会给出编译器错误。即使集合中只有一个函数!

      var bads = new []  // COMPILER ERROR
      {
          Foo
      };
      
      var goods = new Action[]  // NO COMPILER ERROR
      {
          Foo
      };
      
      //...
      public void Foo() { }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-22
        • 2014-12-28
        • 1970-01-01
        • 2021-09-15
        • 1970-01-01
        • 2022-12-02
        • 1970-01-01
        相关资源
        最近更新 更多