创建表达式< Func< T,TKey>>.动态地 [英] Create Expression<Func<T, TKey>> dynamically

查看:273
本文介绍了创建表达式< Func< T,TKey>>.动态地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与以下内容相关:创建具有3个条件的Lambda表达式

请考虑以下GroupBy语句:

group r by new { r.EmployeeID, r.Name }

如果我想在GroupBy语句上方编写Lambda版本并将Expression<Func<T, TKey>>作为参数传递给它,如何创建Expression<Func<T, TKey>>?

If I want to write above GroupBy statement with Lambda version and pass Expression<Func<T, TKey>> to it as parameter, How I can create Expression<Func<T, TKey>>?

推荐答案

我没有时间向您介绍整个过程,但是总体要点是,您将使用类.

I don't have time to walk you through the whole thing, but the general gist is that you'll construct expressions using static methods off of the Expression class.

例如,通过将构造函数信息和一系列表达式传递给Expression.New()重载之一来创建new表达式.

For example, the new expression will be created by passing constructor information and a series of expressions and such to one of the Expression.New() overloads.

r => ...的概念由Expression.Lambda<Func<T, TKey>>(...)体现,您在其中传递Expression.Parameter(typeof(T), "r")作为lambda的参数.

The concept of r => ... is embodied by Expression.Lambda<Func<T, TKey>>(...) where you pass an Expression.Parameter(typeof(T), "r") as the lambda's parameter.

Expression.MakeMemberAccess将帮助您生成r.EmployeeIdr.Name.

我发现以这样的代码在LINQPad中转储示例表达式开始很有用:

I find it useful to start by dumping a sample expression in LINQPad with code like this:

void Main()
{
    DumpExp(r => new { r.EmployeeID, r.Name });
}

public void DumpExp<TKey>(Expression<Func<Foo, TKey>> expr)
{
    expr.Dump();
}

public class Foo
{
    public string EmployeeID;
    public string Name;
}

这将使您可以检查树并了解构成树的哪些类型的表达式以及它们的属性的值.然后,您可以尝试编写匹配的代码.

That will allow you to inspect the tree and understand what types of expressions make up the tree, and what the values of their properties are. Then you can try writing code to match.

一旦获得了您认为可以使用的表达式,就可以在其上调用Compile来生成可运行的函数,该函数可用于测试其在内存对象中的行为.

Once you've got an Expression that you think will work, you can call Compile on it to produce a runnable function, which you can use to test its behavior on in-memory objects.

我猜想您遇到的匿名类型可能是问题中最困难的部分.如果目标框架在其GroupBy语句中支持类似Tuples的内容,那么最好改用它们.

I'm guessing the anonymous type you've got there may be the most difficult part of the problem. If the target framework supports something like Tuples in its GroupBy statements, you will probably be better off using those instead.

有一个古老的项目,旨在使您能够运行通过向类似LINQ的方法提供字符串而构建的动态LINQ语句.它称为动态LINQ",它可能满足您的需求.

There is an oldish project that was designed to enable you to run dynamic LINQ statements built by providing strings to LINQ-like methods. It's called "Dynamic LINQ", and it might suit your needs.

您可能还会发现此库很有用.它应该可以帮助您使用更流畅的语法来构建Expression树.但是,它似乎没有在积极开发中.

You might also find this library useful. It's supposed to help you build Expression trees with a more fluent syntax. However, it doesn't appear to be under active development.

这篇关于创建表达式&lt; Func&lt; T,TKey&gt;&gt;.动态地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆