zoukankan      html  css  js  c++  java
  • Lambda表达式公共拼接函数(原创)

     1 #region Lambda公共拼接函数
     2 /// <summary>
     3 /// LambdaWhere(枚举)
     4 /// </summary>
     5 public enum LambdaWhere : int
     6 {
     7   And = 1,
     8   Or = 2
     9 }
    10 /// <summary>
    11 /// Lambda公共拼接函数(来自:http://www.cnblogs.com/CielWater)
    12 /// </summary>
    13 /// <param name="BodyLeft">要拼接的Lambda表达式</param>
    14 /// <param name="BodyRight">要拼接的Lambda表达式</param>
    15 /// <param name="LambdaWhere">拼接方式,And或者Or</param>
    16 /// <returns></returns>
    17 public Expression<Func<T, bool>> Lambda<T>(Expression<Func<T, bool>> BodyLeft, Expression<Func<T, bool>> BodyRight, LambdaWhere LambdaWhere)
    18 {
    19   if (BodyLeft != null && BodyRight != null)
    20   {
    21     ParameterExpression pe = BodyLeft.Parameters.Single();
    22     switch (LambdaWhere)
    23     {
    24       case LambdaWhere.And:
    25         BodyLeft = Expression.Lambda<Func<T, bool>>(
    26         Expression.And(BodyLeft.Body, Expression.Invoke(BodyRight, pe)), pe);
    27         break;
    28       default:
    29         BodyLeft = Expression.Lambda<Func<T, bool>>(
    30         Expression.Or(BodyLeft.Body, Expression.Invoke(BodyRight, pe)), pe);
    31         break;
    32     }
    33   }
    34   else if (BodyLeft != null)
    35   {
    36     return BodyLeft;
    37   }
    38   else
    39   {
    40     return BodyRight;
    41   }
    42   return BodyLeft;
    43 }
    44 #endregion
  • 相关阅读:
    可视化地图(从省级下钻到市级)
    全国疫情统计可视化地图
    |和||、&&和&
    MFC 常见问题
    * 和-> 优先级
    MFC控件CTabCtrl关联变量
    C++ #include—尖括号和双引号的区别
    C++类型转换
    VC++生成不同的随机数
    VS 2008 头文件库文件设置
  • 原文地址:https://www.cnblogs.com/CielWater/p/3476975.html
Copyright © 2011-2022 走看看