c# - ASP.NET LINQ Error Cannot create a query result of type 'System.Collections.Generic.List`1[System.Int32]' -


i trying create dictionary<string, list<int>> through linq statement

it gives me following error: cannot create query result of type 'system.collections.generic.list`1[system.int32]'

        var output = (                     e in edc.energideklarationer_as                     municipalityname.contains(e.municipality)                     group e e.municipality g                     select new                     {                         municipality = g.key,                          listan = new list<int>()                         {                         g.count(e => e.h== "el"),                         g.count(e => e.h== "eldningsolja"),                         g.count(e => e.h== "flis"),                         g.count(e => e.h== "markvarmepump"),                         g.count(e => e.h== "ved")                         }                     }             ).todictionary(x => x.municipality, x=> x.listan); 

i guess you're using kind of orm library , edc.energideklarationer_as object iqueryable implemented in orm. happens library trying generate query (maybe sql query, depending on library you're using) underlying data source linq query.

some expressions not supported in orms - example entity framework won't handle select new int[] { ... }, handle select new list<int> { ... }. seems library you're using not support creating new list in query. , exception thrown in todictionary, because that's result instantiated , linq gets translated.

try replacing list:

                listan = new list<int>()                 {                     g.count(e => e.h== "el"),                     g.count(e => e.h== "eldningsolja"),                     g.count(e => e.h== "flis"),                     g.count(e => e.h== "markvarmepump"),                     g.count(e => e.h== "ved")                 } 

with anonymous type:

                listan = new                  {                     el = g.count(e => e.h== "el"),                     eldningsolja = g.count(e => e.h== "eldningsolja"),                     flis = g.count(e => e.h== "flis"),                     markvarmepump = g.count(e => e.h== "markvarmepump"),                     ved = g.count(e => e.h== "ved")                 } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -