Anyname Donotcare
Anyname Donotcare

Reputation: 11393

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 AsEnumerable() method in my case .

resultList = conn.Query(query.ToString(),
               new
               {
                  years = new[] {year, year - 1, year - 2},
                  yearsLimit = new[]
                  {
                     year,
                     year - 1
                  },
                  PeriodTypeId = periodTypeId,
                  Period = period
               }).AsEnumerable().
            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(),

               }).FirstOrDefault()

            }).ToList();

Upvotes: 2

Views: 2637

Answers (1)

KMoussa
KMoussa

Reputation: 1578

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.

Upvotes: 1

Related Questions