我可以使用 LINQ 表达式参数化 PropertyExpression 的属性名称吗?

发布于 2024-12-26 12:27:11 字数 808 浏览 4 评论 0原文

假设我有以下 LambdaExpression:

var itemParam = Expression.Parameter(typeof(Thing), "thing");
var someValue = "ABCXYZ123"; // value to compare

LambdaExpression lex = Expression.Lambda(
  Expression.Equal(
    Expression.Property(itemParam, "Id"), // I want ID to be a Body expression parameter
    Expression.Constant(someValue)
  ), itemParm);

并且我希望将 Expression.Property(...) 工厂中的属性名称(第二个参数)作为参数,我该如何执行此操作?

我希望看到一个看起来像这样的构造函数,但它不存在:

Expression.Property(Expression instance, Expression propName)

是否有一些我可以使用的技巧,可以将参数化的 ConstantExpression 转换为所需的字符串或会员信息?也许我的处理方式是错误的。

我的预感是,由于这些表达式树在编译时会成为轻量级 IL,因此需要成员访问信息,因此在构造表达式树时必须提供成员和属性的名称。

感谢您的任何提示!

编辑:想补充一点,这将用作 Enumerable.Where(...) 扩展方法的参数,用于确定两个类/实体之间关系的匹配。

Supposing I have the following LambdaExpression:

var itemParam = Expression.Parameter(typeof(Thing), "thing");
var someValue = "ABCXYZ123"; // value to compare

LambdaExpression lex = Expression.Lambda(
  Expression.Equal(
    Expression.Property(itemParam, "Id"), // I want ID to be a Body expression parameter
    Expression.Constant(someValue)
  ), itemParm);

And I want the property name (2nd parameter) in the Expression.Property(...) factory to be a parameter, how do I go about doing this?

I was hoping to see a constructor that looks like this, but it doesn't exist:

Expresssion.Property(Expression instance, Expression propName)

Is there some trick I can pull that can convert a parameterized ConstantExpression into the needed string or MemberInfo? Maybe I'm going about this the wrong way.

My hunch is that because these expression trees, when compiled, become lightweight IL, that member access information is required, thus, the names of members and properties must be provided when constructing expression trees.

Thanks for any tips!!

EDIT: Wanted to add that this will be used as an argument to the Enumerable.Where(...) extension method for determining a match on a relationship between two classes / entities.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2025-01-02 12:27:11

表达式树表示的 IL 结构大致与您在 C#/VB.NET 程序中看到的类型相同。您无法参数化该属性表达式,其原因与您无法“参数化”以下代码的原因相同:

var x = new MyClass {Id = 1, Name = "hello"};
var propName = "Id";
var xId = x.propName; // <-- This will not compile

如果您需要实现此功能,并且您的表达式树未传递给 IQueryable,您可以编写一个辅助函数,它接受一个对象和一个字符串,并返回由该字符串标识的该对象的属性值;然后您可以使用Expression.Call 将该辅助函数调用到您正在构建的表达式树中。

Expression trees represent IL structures that are roughly of the same kind that you see in your C#/VB.NET programs. You cannot parameterize that property expression for the same reason that you cannot "parameterize" the following code:

var x = new MyClass {Id = 1, Name = "hello"};
var propName = "Id";
var xId = x.propName; // <-- This will not compile

If you need to implement this functionality, and your expression trees are not passed to IQueryable<T>, you can write a helper function taking an object and a string, and returning value of that object's property identified by the string; then you can use Expression.Call to invoke that helper function into the expression tree that you are building.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文