C#實現通用資料過濾表單

來源:互聯網
上載者:User

標籤:opera   rem   exce   stat   實值型別   資料   field   必須   info   

/// <summary> /// 擷取查詢運算式樹 (zuowenjun.cn) /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="fieldName"></param> /// <param name="operatorName"></param> /// <param name="value"></param> /// <param name="value2"></param> /// <returns></returns> public  static  Expression<Func<TEntity,  bool >> GetQueryExpression<TEntity>( string  fieldName,  string  operatorName,  string  value,  string  value2)  where  TEntity :  class {      PropertyInfo fieldInfo =  typeof (TEntity).GetProperty(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);      Type pType = fieldInfo.PropertyType;      if  ( string .IsNullOrEmpty(operatorName))      {          throw  new  ArgumentException( "運算子不可為空!" "operatorName" );      }      dynamic convertedValue;      if  (!value.TryChangeType(pType,  out  convertedValue))      {          throw  new  ArgumentException( string .Format( "【{0}】的查詢實值型別不正確,必須為{1}類型!" , General.GetDisplayName(fieldInfo), pType.FullName),  "value" );      }      ParameterExpression expParameter = Expression.Parameter( typeof (TEntity),  "f" );      MemberExpression expl = Expression.Property(expParameter, fieldInfo);      ConstantExpression expr = Expression.Constant(convertedValue, pType);      Expression expBody =  null ;      Type expType =  typeof (Expression);      var  expMethod = expType.GetMethod(operatorName,  new [] { expType, expType });      if  (expMethod !=  null )      {          expBody = (Expression)expMethod.Invoke( null new  object [] { expl, expr });      }      else  if  (FilterOperators.Between == operatorName)      {          dynamic convertedValue2;          if  (!value2.TryChangeType(pType,  out  convertedValue2))          {              throw  new  ArgumentException( string .Format( "【{0}】的查詢值2類型不正確,必須為{1}類型!" , General.GetDisplayName(fieldInfo), pType.FullName),  "value" );          }          ConstantExpression expr2 = Expression.Constant(convertedValue2, pType);          expBody = Expression.GreaterThanOrEqual(expl, expr);          expBody = Expression.AndAlso(expBody, Expression.LessThanOrEqual(expl, expr2));      }      else  if  ( new [] { FilterOperators.Contains, FilterOperators.StartsWith, FilterOperators.EndsWith }.Contains(operatorName))      {          expBody = Expression.Call(expl,  typeof ( string ).GetMethod(operatorName,  new  Type[] {  typeof ( string ) }), expr);      }      else      {          throw  new  ArgumentException( "無效的運算子!" "operatorName" );      }      Expression<Func<TEntity,  bool >> lamExp = Expression.Lambda<Func<TEntity,  bool >>(expBody, expParameter);      return  lamExp; }

C#實現通用資料過濾表單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.