How to create LINQ Expression Tree to select an anonymous type

前端 未结 9 662
夕颜
夕颜 2020-11-22 12:12

I would like to generate the following select statement dynamically using expression trees:

var v = from c in Countries
        where c.City == \"London\"
           


        
相关标签:
9条回答
  • 2020-11-22 12:39

    I don't believe that you will be able to achieve this. Although when you do select new { c.Name, c.Population } it seems like you're not creating a class you actually are. If you have a look at the compiled output in Reflector or the raw IL you will be able to see this.

    You'll have a class which would look something like this:

    [CompilerGenerated]
    private class <>c__Class {
      public string Name { get; set; }
      public int Population { get; set; }
    }
    

    (Ok, I cleaned it up a touch, since a property is really just a get_Name() and set_Name(name) method set anyway)

    What you're trying to do is proper dynamic class creation, something which wont be available until .NET 4.0 comes out (and even then I'm not really sure if it'll be able to achieve what you want).

    You're best solution would be to define the different anonymous classes and then have some kind of logical check to determine which one to create, and to create it you can use the object System.Linq.Expressions.NewExpression.

    But, it may be (in theory at least) possible to do it, if you're getting really hard-core about the underlying LINQ provider. If you are writing your own LINQ provider you can detect if the currently-parsed expression is a Select, then you determine the CompilerGenerated class, reflect for its constructor and create.

    Defiantly not a simple task, but it would be how LINQ to SQL, LINQ to XML, etc all do it.

    0 讨论(0)
  • 2020-11-22 12:41

    The accepted answer is very useful, but I needed something a little closer to a real anonymous type.

    A real anonymous type has read-only properties, a constructor for filling in all of the values, an implementation of Equals/GetHashCode for comparing the values of each property, and an implementation ToString that includes the name/value of each property. (See https://msdn.microsoft.com/en-us/library/bb397696.aspx for a full description of anonymous types.)

    Based on that definition of anonymous classes, I put a class that generates dynamic anonymous types on github at https://github.com/dotlattice/LatticeUtils/blob/master/LatticeUtils/AnonymousTypeUtils.cs. The project also contains some unit tests to make sure the fake anonymous types behave like real ones.

    Here's a very basic example of how to use it:

    AnonymousTypeUtils.CreateObject(new Dictionary<string, object>
    {
        { "a", 1 },
        { "b", 2 }
    });
    

    Also, another note: I found that when using a dynamic anonymous type with Entity Framework, the constructor must be called with the "members" parameter set. For example:

    Expression.New(
        constructor: anonymousType.GetConstructors().Single(), 
        arguments: propertyExpressions,
        members: anonymousType.GetProperties().Cast<MemberInfo>().ToArray()
    ); 
    

    If you used one of the versions of Expression.New that does not include the "members" parameter, Entity Framework would not recognize it as the constructor of an anonymous type. So I assume that means a real anonymous type's constructor expression would include that "members" information.

    0 讨论(0)
  • 2020-11-22 12:43

    Maybe a bit late but may help to someone.

    You Can generate dynamic select by call DynamicSelectGenerator in select from an entity.

    public static Func<T, T> DynamicSelectGenerator<T>()
                {
                    // get Properties of the T
                    var fields = typeof(T).GetProperties().Select(propertyInfo => propertyInfo.Name).ToArray();
    
                // input parameter "o"
                var xParameter = Expression.Parameter(typeof(T), "o");
    
                // new statement "new Data()"
                var xNew = Expression.New(typeof(T));
    
                // create initializers
                var bindings = fields.Select(o => o.Trim())
                    .Select(o =>
                    {
    
                        // property "Field1"
                        var mi = typeof(T).GetProperty(o);
    
                        // original value "o.Field1"
                        var xOriginal = Expression.Property(xParameter, mi);
    
                        // set value "Field1 = o.Field1"
                        return Expression.Bind(mi, xOriginal);
                    }
                );
    
                // initialization "new Data { Field1 = o.Field1, Field2 = o.Field2 }"
                var xInit = Expression.MemberInit(xNew, bindings);
    
                // expression "o => new Data { Field1 = o.Field1, Field2 = o.Field2 }"
                var lambda = Expression.Lambda<Func<T, T>>(xInit, xParameter);
    
                // compile to Func<Data, Data>
                return lambda.Compile();
            }
    

    And use by this code:

    var result = dbContextInstancs.EntityClass.Select(DynamicSelectGenerator<EntityClass>());
    
    0 讨论(0)
  • 2020-11-22 12:46

    You could use the Dynamic Expression API which allows you to dynamically build your select statement like this:

     Select("new(<property1>,<property2>,...)");
    

    You need the Dynamics.cs file from the LINQ and language samples for Visual Studio for this to work, both are linked at the bottom of this page. You can also see a working example showing this in action on at the same URL.

    0 讨论(0)
  • 2020-11-22 12:50

    I think most of the things are already answered - as Slace said, you need some class that would be returned from the Select method. Once you have the class, you can use the System.Linq.Expressions.NewExpression method to create the expression.

    If you really want to do this, you can generate class at runtime too. It's a bit more work, because it cannot be done using LINQ Expression trees, but it's possible. You can use System.Reflection.Emit namespace to do that - I just did a quick search and here is an article that explains this:

    • Introduction to Creating Dynamic Types with Reflection.Emit
    0 讨论(0)
  • 2020-11-22 12:52

    This compiles, I dunno if it works however...

    myEnumerable.Select((p) => { return new { Name = p.Name, Description = p.Description }; });
    

    Assuming p is what your transforming, and the select statement is returning an anon type, using the function declaration of lambda's.

    Edit: I also don't know how you would generate this dynamically. But at least it shows you how to use the select lambda to return an anon type with multiple values

    Edit2:

    You would also have to bare in mind, that the c# compiler actually generates static classes of the anon type. So the anon type does actually have a type after compile time. So if your generating these queries at run time (which I assume you are) you may have to construct a type using the various reflection methods (I believe you can use them to make types on the fly) load the created types into execution context and use them in your generated output.

    0 讨论(0)
提交回复
热议问题