Expression expression Tree, Expression expression Expression
Expression TreeCode indicating the tree data structureEach node in the tree structure is an expression, such as a method call or a binary operation similar to x <y
1. Create an expression tree using Lambda expressions
Expression<Func<int, int, int, int>> expr = (x, y, z) => (x + y) / z;
2. Compile the expression tree,This method compiles the Code represented by the expression tree into an executable delegate.
expr.Compile()(1, 2, 3)
3. IQueryable <T> extension method, implementation of WhereIn
var d = list.AsQueryable().WhereIn(o => o.Id1, new int[] { 1, 2 });
Complete code:
Using MongoDB. bson; using MongoDB. driver; using MongoDB. driver. builders; using System. collections. generic; using System. linq; using System. linq. expressions; using System. text; using System. threading. tasks; namespace MongoDBTest {class Program {static void Main (string [] args) {// use LambdaExpression to construct the Expression Tree Expression <Func <int, int> expr = (x, y, z) => (x + y)/z; Console. writeLine (ex Pr. compile () (1, 2, 3); // use LambdaExpression to construct the executable code Func <int, int> fun = (x, y, z) => (x + y)/z; Console. writeLine (fun (1, 2, 3); // dynamically build the Expression Tree ParameterExpression pe1 = Expression. parameter (typeof (int), "x"); ParameterExpression pe2 = Expression. parameter (typeof (int), "y"); ParameterExpression pe3 = Expression. parameter (typeof (int), "z"); var body = Expression. divide (Expression. add (pe1, Pe2), pe3); var w = Expression. lambda <Func <int, int> (body, new ParameterExpression [] {pe1, pe2, pe3}); Console. writeLine (w. compile () (1, 2, 3); List <Entity> list = new List <Entity> {new Entity {Id1 = 1}, new Entity {Id1 = 2 }, new Entity {Id1 = 3 }}; var d = list. asQueryable (). whereIn (o => o. id1, new int [] {1, 2}); d. toList (). forEach (o => {Console. writeLine (o. id1) ;}); Conso Le. readKey () ;}} public class Entity {public ObjectId Id; public int Id1; public string Name {get; set ;}} public static class cc {public static IQueryable <T> WhereIn <T, TValue> (this IQueryable <T> query, Expression <Func <T, TValue> obj, IEnumerable <TValue> values) {return query. where (BuildContainsExpression (obj, values);} private static Expression <Func <TElement, bool> BuildContainsExpress Ion <TElement, TValue> (Expression <Func <TElement, TValue> valueSelector, IEnumerable <TValue> values) {if (null = valueSelector) {throw new ArgumentNullException ("valueSelector");} if (null = values) {throw new ArgumentNullException ("values");} var p = valueSelector. parameters. single (); if (! Values. any () return e => false; var equals = values. select (value => (Expression) Expression. equal (valueSelector. body, Expression. constant (value, typeof (TValue); var body = equals. aggregate (Expression. or); return Expression. lambda <Func <TElement, bool> (body, p );}}}
Refer to blog:
C # Expression Tree ExpressionCall
Please write out a function Declaration, that is, the function name, parameter, and return value type. I will provide you with additional definitions.
C # Expression Tree
Delegate (T)
{
Return T. id = 2;
}