Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: An expression tree may not contain a dynamic operation

I use Asp.Net 4 and C#, I use EF 4.

I have this query, I receive an error:

 An expression tree may not contain a dynamic operation

dynamic o = e.Item.DataItem;
var imagesContent = context.CmsImagesContents.FirstOrDefault(img => img.ContentId == o.ContentId);

It seems is imposible to Cast a Dynamic Type using a Lamba Expression.

How I can fix the problem, and able to use my object o in my Lamba? Thanks

PS: e.Item.DataItem is of Type CmsContent and o.ContentId is of type Int

like image 484
GibboK Avatar asked Aug 19 '11 07:08

GibboK


2 Answers

Unboxing the object will do the trick:

     int contentId = (int)o.ContentId;
     var image = context.CmsImagesContents.FirstOrDefault(img => img.ContentId == contentId);

For more info about 'boxing/unboxing' click here

like image 146
GibboK Avatar answered Nov 18 '22 01:11

GibboK


Change

dynamic o = e.Item.DataItem;

To

var o = (CmsContent)e.Item.DataItem;
like image 4
cdhowie Avatar answered Nov 18 '22 01:11

cdhowie