Linq 与自定义通用扩展方法结合构建错误 - 表达式树可能不包含赋值运算符?
我创建了一个通用扩展方法,该方法对对象执行操作并返回该对象:
public static T Apply<T>(this T subject, Action<T> action)
{
action(subject);
return subject;
}
我无法在 EntityFramework Linq 查询中使用此扩展方法,因为:
表达式树可能不包含赋值 这是为什么呢
?
Linq 查询:
var parents = from p in context.Parent
join phr in context.Child on p.key equals phr.parentkey
into pr
select p.Apply(
x => x.Children = //The assignment operator that fails to build...
pr.ToDictionary(y => y.childkey, y => y.childname));
I've made a generic extension method that executes an action on an object and returns the object after that:
public static T Apply<T>(this T subject, Action<T> action)
{
action(subject);
return subject;
}
I'm unable to use this extension method in an EntityFramework Linq query because of:
An expression tree may not contain an assignment operator
Why is this?
The Linq query:
var parents = from p in context.Parent
join phr in context.Child on p.key equals phr.parentkey
into pr
select p.Apply(
x => x.Children = //The assignment operator that fails to build...
pr.ToDictionary(y => y.childkey, y => y.childname));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,撇开赋值运算符不谈,您希望如何将
Apply
方法转换为 SQL?实体框架对此一无所知,也无法深入研究不透明委托。我怀疑您真正需要做的是将数据库中执行的操作与本地执行的操作分开:
Well, assignment operator aside, how would you expect your
Apply
method to be translated into SQL? Entity Framework doesn't know anything about it, and can't delve into opaque delegates, either.I suspect what you really need to do is separate out the bits to do in the database from the bits to do locally: