With AsEnumerable, you can't use [] to index an expression of type 'object' ()

asp.net c# dapper entity-framework linq

Question

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();
1
0
11/23/2016 8:34:48 PM

Accepted Answer

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.

1
5/23/2017 12:13:44 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow