• C#动态拼接Linq


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    
    namespace DynamicLinqDemo
    {
        public class Person
        {
            public string Name { get; set; }
        }
    
        /// <summary>
        ///     动态拼接Linq
        ///     https://www.cnblogs.com/LifeDecidesHappiness/p/15270834.html
        ///     LDH @ 2021-9-15
        /// </summary>
        internal class Program
        {
            private static void Main()
            {
                Console.Title = "动态拼接Linq";
    
                DynamicLinq();
    
                Console.ReadKey();
            }
    
            /// <summary>
            ///     动态拼接Linq
            /// </summary>
            private static void DynamicLinq()
            {
                var listPeople = new List<Person>();
                var p = new Person { Name = "LDH" };
                listPeople.Add(p);
    
                Console.WriteLine("Person.Name = " + p.Name);
                PrintLine();
    
                var filterCondition = GetExpression<Person>("x", "Contains", "Name", "LD");
                Console.WriteLine("Contains 动态 Linq:" + filterCondition); //x => x.Name.Contains("LD")
    
                var expression = listPeople.Where(filterCondition.Compile());
                Console.WriteLine("Contains Count:" + expression.Count());
                PrintLine();
    
                var filterCondition2 = GetExpression<Person>("x", "StartsWith", "Name", "LD");
                Console.WriteLine("StartsWith 动态 Linq:" + filterCondition2); //x => x.Name.StartsWith("LD")
    
                var expression2 = listPeople.Where(filterCondition2.Compile());
                Console.WriteLine("StartsWith Count:" + expression2.Count());
                PrintLine();
    
                var filterCondition3 = GetExpression<Person>("x", "EndsWith", "Name", "LD");
                Console.WriteLine("EndsWith 动态 Linq:" + filterCondition3); //x => x.Name.EndsWith("LD")
    
                var expression3 = listPeople.Where(filterCondition3.Compile());
                Console.WriteLine("EndsWith Count:" + expression3.Count());
                PrintLine();
    
                var filterCondition4 = GetExpression<Person>("x", "Equals", "Name", "LDH");
                Console.WriteLine("Equals 动态 Linq:" + filterCondition4); //x => x.Name.Equals("LDH")
    
                var expression4 = listPeople.Where(filterCondition4.Compile());
                Console.WriteLine("Equals Count:" + expression4.Count());
                PrintLine();
            }
    
    
            /// <summary>
            ///     动态拼接Linq
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="lambdaVariableName">Lambda变量名(eg: x,m,n,type)</param>
            /// <param name="operatorName">StartsWith,Contains,EndsWith</param>
            /// <param name="propertyName">属性名称</param>
            /// <param name="propertyValue">属性值</param>
            /// <returns>eg:x => x.Name.Contains("LD")</returns>
            private static Expression<Func<T, bool>> GetExpression<T>(string lambdaVariableName, string operatorName,
                string propertyName, string propertyValue)
            {
                var parameterExp = Expression.Parameter(typeof(T), lambdaVariableName);
                var propertyExp = Expression.Property(parameterExp, propertyName);
    
                var method = typeof(string).GetMethod(operatorName, new[] { typeof(string) });
                var someValue = Expression.Constant(propertyValue, typeof(string));
    
                var containsMethodExp = Expression.Call(propertyExp, method ?? throw new InvalidOperationException(), someValue);
                return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
            }
    
            /// <summary>
            ///     分割线
            /// </summary>
            private static void PrintLine()
            {
                Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
            }
        }
    }
    踏实做一个为人民服务的搬运工!
  • 相关阅读:
    Linux命令行界面使用代理上网
    .NET 开发框架 代码生成器
    如何正确地学习
    Ubuntu实用命令——不断更新中......
    MSSQL如何快速清除数据库日志转,经实践有效
    C# 获取机器码
    C#中得到每周,每月,每季,每年的年初末日期
    asp.net(C#)解析Json的类代码
    由拖库攻击谈口令字段的加密策略(数据库加密)
    用sql查询当天,一周,一个月的数据
  • 原文地址:https://www.cnblogs.com/LifeDecidesHappiness/p/15270834.html
Copyright © 2020-2023  润新知