Error received "No best type found for implicitly-typed array"
Asked Answered
A

4

6

This is my code for creating Json response for jqGrid and for new keyword for defining cell member I receive following message "No best type found for implicitly-typed array".

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

What am I doing wrong here?

Aerugo answered 28/6, 2012 at 12:37 Comment(0)
D
8

Assuming Reference, Currency1 and Currency2 are strings, just declare it as a string array:

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();
Dillondillow answered 28/6, 2012 at 13:13 Comment(1)
@D Stanley, I did it and it works! Actually, if these were not strings, I could just use cell = new object [] {... }.Franglais
K
1

If you prepare data for jqGrid (like in your code), you can define your own jsonReader and just skip the cell array (http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data):

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

Then something like:

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()
}
Knipe answered 8/9, 2013 at 10:59 Comment(1)
Replace "select new" to "new"Barracks
L
0

I had this same issue and found that if all data items in the array were of the same type (example String) then the type was inferred and the compiler didn't complain about new[].

Lobell answered 21/4, 2013 at 18:16 Comment(0)
B
0

If the members of the collection are functions, it still gives the compiler error. Even if there is only one function in the collection!

var bads = new []  // COMPILER ERROR
{
    Foo
};

var goods = new Action[]  // NO COMPILER ERROR
{
    Foo
};

//...
public void Foo() { }
Bennir answered 7/7, 2017 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.