父级导航属性的 C# 表达式树参数
如何为 1 到 * 导航属性的父端创建 ParameterExpression?
以下内容适用于子实体:
var parameter = Expression.Parameter(
typeof(T), // where T is the entity type
GetParameterName()); // helper method to get alias
在 TParent 上尝试类似的操作会生成源自 Context 的查询,而不是作为子实体的属性。
lambda 等效项将如下所示:
var q = from f in context.Foo
where f.Bar.BarId == 1...
// where bar is the Navigation Property to the parent
编辑清楚:
我使用以下内容从属性创建成员表达式:
Expression exp = Expression.Equal(
Expression.Property(parameter, "SomeColumn"),
Expression.Constant("SomeValue"));
因此,在这种情况下,看起来我应该使用 MemberExpression 而不是 ParameterExpression。
How can I create a ParameterExpression for the parent side of a 1 to * Navigation Property?
The following works for the child entity:
var parameter = Expression.Parameter(
typeof(T), // where T is the entity type
GetParameterName()); // helper method to get alias
Trying something similar on TParent produces a query originating at the Context, and not as a property on the child.
The lambda equivalent would be like this:
var q = from f in context.Foo
where f.Bar.BarId == 1...
// where bar is the Navigation Property to the parent
Edit for clarity:
I use the following to create a member expression from a property:
Expression exp = Expression.Equal(
Expression.Property(parameter, "SomeColumn"),
Expression.Constant("SomeValue"));
So it looks like I should be using MemberExpression instead of ParameterExpression for this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有点困惑...“.Bar”不是一个
ParameterExpression
- 它是一个MemberExpression
。f
是参数。 你到底想做什么?请注意,一种选择是将示例代码加载到反射器中,打开 .NET 3.5 提示,并阅读它是如何执行的 - 它看起来像代码 此处,但通常很容易遵循。
I'm a little confused... ".Bar" isn't a
ParameterExpression
- it is aMemberExpression
.f
is the parameter. What exactly is it that you want to do?Note that one option is to load the sample code into reflector, turn of the .NET 3.5 hints, and read how it does it - it'll look like the code here, but is usually easy enough to follow.