如何用表达式树编写linq

如何用表达式树编写linq

本文介绍了如何用表达式树编写linq .join来进行动态查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在任何地方都找不到答案...

I Can't seem to find the answer to this anywhere...

目标是找到一种使用表达式树语句执行linq .join()的方法.

The objective is to find a way to do linq .join() using Expression tree statements.

所以...使用 Microsoft示例:

class Person
{
    public string Name { get; set; }
}

class Pet
{
    public string Name { get; set; }
    public Person Owner { get; set; }
}

public static void JoinEx1()
{
    Person magnus = new Person { Name = "Hedlund, Magnus" };
    Person terry = new Person { Name = "Adams, Terry" };
    Person charlotte = new Person { Name = "Weiss, Charlotte" };



   Pet barley = new Pet { Name = "Barley", Owner = terry };
    Pet boots = new Pet { Name = "Boots", Owner = terry };
    Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
    Pet daisy = new Pet { Name = "Daisy", Owner = magnus };


List<Person> people = new List<Person> { magnus, terry, charlotte };
List<Pet> pets = new List<Pet> { barley, boots, whiskers, daisy };

// Join the list of Person objects and the list of Pet objects
// to create a list of person-pet pairs where each element is
// an anonymous type that contains the name of pet and the name
// of the person that owns the pet.
var query = people.AsQueryable().Join(pets,
                person => person,
                pet => pet.Owner,
                (person, pet) =>
                    new { OwnerName = person.Name, Pet = pet.Name });

任何人都可以使用表达树?

推荐答案

由于Queryable.Join是一种通用的静态方法,因此最容易使用扩展方法为所需的MethodInfo查找内容.

Since Queryable.Join is a generic static method, it is easiest to use an extension method to lookup the MethodInfo for the one you need:

public static class TypeExt {
    public static MethodInfo GetMethod(this Type t, string methodName, int paramCount) =>
        t.GetMethods().Where(mi => mi.Name == methodName && mi.GetParameters().Length == paramCount).Single();
}

Join方法采用五个参数(扩展方法将它们所应用的对象作为第一个参数传递),因此我们将一次将它们构建为Expression的一个.五个参数是IQueryable<>,要联接的IEnumerable<>,外键选择器lambda,内键选择器lambda和结果选择器lambda.

The Join method takes five parameters (extension methods pass the object they are applied to as the first parameter), so we will build them as Expressions one at a time. The five parameters are the IQueryable<>, the IEnumerable<> to join, the outer key selector lambda, the inner key selector lambda and the result selector lambda.

// Build Queryable.Join<TOuter,TInner,TKey,TResult> and use as query expression

// IQueryable<TOuter>
var arg0 = Expression.Constant(people.AsQueryable());

// IEnumerable<TInner>
var arg1 = Expression.Constant(pets);

// TOuter person
var arg2p = Expression.Parameter(people.GetType().GetGenericArguments()[0], "person");
// also TKey person
// Expression<Func<TOuter,TKey>>: person => person
var arg2 = Expression.Quote(Expression.Lambda(arg2p, arg2p));

// TInner pet
var arg3p = Expression.Parameter(pets.GetType().GetGenericArguments()[0], "pet");
// TKey pet.Owner
var arg3body = Expression.Property(arg3p, "Owner");
// Expression<Func<TInner,TKey>>: pet => pet.Owner
var arg3 = Expression.Quote(Expression.Lambda(arg3body, arg3p));

// TResult = typeof(new { string OwnerName , string Pet })
var anonymousType = (new { OwnerName = default(string), Pet = default(string) }).GetType();
// .ctor
var arg4Constructor = anonymousType.GetConstructors()[0];
// person.Name
var arg4PersonName = Expression.Property(arg2p, "Name");
// pet.Name
var arg4PetName = Expression.Property(arg3p, "Name");
var arg4Args = new[] { arg4PersonName, arg4PetName };
// new[] { .OwnerName, .Pet }
var arg4Members = anonymousType.GetProperties();
// new { OwnerName = person.Name, Pet = pet.Name }
var arg4body = Expression.New(arg4Constructor, arg4Args, arg4Members);
// Expression<Func<TOuter,TInner,TResult>>: (person,pet) => new { OwnerName = person.Name, Pet = pet.Name }
var arg4 = Expression.Quote(Expression.Lambda(arg4body, arg2p, arg3p));

注意:出于复杂的嵌套lambda关闭原因,每个lambda都由Expression.Quote包围,因此Expression编译器将知道返回Expression树而不是委托.在此示例中,没有区别.

Note: For complicated nested lambda closure reasons, each lambda is surrounded by Expression.Quote so the Expression compiler will know to return an Expression tree and not a delegate. In this example it makes no difference.

现在,使用扩展方法,您可以查找所需的Join,将其从通用方法实例化为要查询的类型的特定方法,然后创建Join方法调用表达式:

Now using the extension method you can lookup the Join you need to use, instantiate it from a generic method to the specific method for the types you are querying, and create the Join method call expression:

var joinGenericMI = typeof(Queryable).GetMethod("Join", 5);
var joinMI = joinGenericMI.MakeGenericMethod(new[] { arg2p.Type, arg3p.Type, arg2.ReturnType, anonymousType });
var qExpr = Expression.Call(joinMI, arg0, arg1, arg2, arg3, arg4);

最后,您可以使用Expression创建一个IQueryable<>:

Finally, you can create an IQueryable<> with the Expression:

var q2 = people.AsQueryable().Provider.CreateQuery(qExpr);

这篇关于如何用表达式树编写linq .join来进行动态查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:01