Error: An expression tree may not contain a dynamic operation

孤人 提交于 2019-12-10 01:44:33

问题


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


回答1:


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




回答2:


Change

dynamic o = e.Item.DataItem;

To

var o = (CmsContent)e.Item.DataItem;


来源:https://stackoverflow.com/questions/7118550/error-an-expression-tree-may-not-contain-a-dynamic-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!