Reputation: 125
I have a query that looks something like this
return (from item in context.MERCHANDISE_ITEMS
where item.ORGID == orgId
select new MerchandiseModel()
{
Description = item.DESCRIPTION,
...,
Images = context.MERCHANDISE_IMAGES.Where(i => i.ITEMID == item.ID).ToDictionary(i => i.ID, i => i.IMAGENAME)
}).FirstOrDefault();
Images is a Dictionary and MERCHANDISE_IMAGES.ID is int and MERCHANDISE_IMAGES.IMAGENAME is string.
This is throwing System.NotSupportedException, I've tried doing linq (no lambda), same exception.
Here is the exception:
LINQ to Entities does not recognize the method
'System.Collections.Generic.Dictionary`2[System.Int32,System.String] ToDictionary[MERCHANDISE_IMAGES,Int32,String](System.Collections.Generic.IEnumerable`1[Hylton.Infrastructure.EntityFrameworkORM.MERCHANDISE_IMAGES],
System.Func`2[Hylton.Infrastructure.EntityFrameworkORM.MERCHANDISE_IMAGES,
System.Int32], System.Func`2[Hylton.Infrastructure.EntityFrameworkORM.MERCHANDISE_IMAGES,System.String])' method,
and this method cannot be translated into a store expression.
I have no idea what I'm doing wrong, I've looked through every post on this issue and everything I've seen is doing it the same way. Can anyone point out what I'm missing?
Upvotes: 0
Views: 439
Reputation: 75
Short version: EF can't convert ToDictionary to SQL.
In my experience, the best way to go is to simplify the Select to an anonymous type with all the stuff from the DBOs you'll need, call ToList (or something else to force the evaluation) and then do the ToDictionary and other massaging of the data.
The added benefit of this is by forcing it to evaluate, you'll avoid leaving the using your context is in and closing it, which will cause an error when you try and use the return.
Upvotes: 1