SoFunction
Updated on 2025-03-06

C# Expression expression tree example

using ;
using ;
using ;
using System;
using ;
using ;
using ;
using ;
using ;

namespace MongoDBTest
{
    class Program
    {
        static void Main(string[] args)
        {
//Build expression tree using LambdaExpression
            Expression<Func<int, int, int, int>> expr = (x, y, z) => (x + y) / z;
            (()(1, 2, 3));

//Build executable code using LambdaExpression
            Func<int, int, int, int> fun = (x, y, z) => (x + y) / z;
            (fun(1, 2, 3));

//Dynamic construction of expression trees
            ParameterExpression pe1 = (typeof(int), "x");
            ParameterExpression pe2 = (typeof(int), "y");
            ParameterExpression pe3 = (typeof(int), "z");
            var body = ((pe1, pe2), pe3);
            var w = <Func<int, int, int, int>>(body, new ParameterExpression[] { pe1, pe2, pe3 });
            (()(1, 2, 3));

            List<Entity> list = new List<Entity> { new Entity { Id1 = 1 }, new Entity { Id1 = 2 }, new Entity { Id1 = 3 } };

            var d = ().WhereIn(o => o.Id1, new int[] { 1, 2 });
            ().ForEach(o =>
            {
                (o.Id1);
            });
            ();
        }
    }
    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 (BuildContainsExpression(obj, values));
        }
        private static Expression<Func<TElement, bool>> BuildContainsExpression<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 = ();
            if (!()) return e => false;

            var equals = (value => (Expression)(, (value, typeof(TValue))));
            var body = ();

            return <Func<TElement, bool>>(body, p);
        }
    }
}