Projecting into KeyValuePair via EF / Linq

c# entity-framework keyvaluepair linq projection

Question

I'm attempting to load a collection of KeyValuePairs from the following EF/Linq query:

return (from o in context.myTable 
select new KeyValuePair<int, string>(o.columnA, o.columnB)).ToList();

My issue is that the error stems from this.

"Only parameterless constructors and initializers are supported in LINQ to Entities."

Is there a simple workaround for this? Instead of using KeyValuePair, I could make an own class, but that seems like reinventing the wheel.

1
33
9/11/2018 1:45:48 PM

Accepted Answer

ZZZ_tmp
71
6/25/2013 4:04:30 PM

Popular Answer

Due to the fact that LINQ to Entities does notKeyValuePair you should use LINQ to Object to convert toAsEnumerable first:

return context.myTable
              .AsEnumerable()
              .Select(new KeyValuePair<int, string>(o.columnA, o.columnB))
              .ToList();


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