Anyname Donotcare Anyname Donotcare - 8 months ago 61
ASP.NET (C#) Question

Cannot apply indexing with [] to an expression of type 'object' with AsEnumerable()

I get the following exception :

Cannot apply indexing with [] to an expression of type 'object'

I don't know what's the alternative to
method in my case .

resultList = conn.Query(query.ToString(),
years = new[] {year, year - 1, year - 2},
yearsLimit = new[]
year - 1
PeriodTypeId = periodTypeId,
Period = period
GroupBy(r => r["CompanyId"]).Select(c => new //ERROR
Company = c.Key,
Totals = c.Select(t => new
Total1 = c.Select(t1 => new {Year = t1["Year"], Total1 = t1["Tot1"]}).ToArray(),
Total2 = c.Select(t2 => new {Year = t2["Year"], Total2 = t2["Tot2"]}).ToArray(),
Total3 = c.Select(t3 => new {Year = t3["Year"], Total3 = t3["Tot3"]}).ToArray(),



Answer Source

As per Dapper's Documentation, and as @Technetium noted in the comments, the non-generic version of the Query method retuns IEnumerable<dynamic>

One way to get your code working is to replace .AsEnumerable with .OfType<IDictionary<string, object>> (according to this and that code, it appears that Query actually returns IEnumerable<DapperRow>, and DapperRow inherits from IDictionary<string, object>), otherwise you can access the properties directly on the returned dynamic (e.g. r.CompanyId vs r["CompanyId"])

Also, here is an answer to a similar question on SO, for reference.