Util应用程序框架公共操作类(八):Lambda表达式公共操作类(二)

时间:2021-08-08 09:43:19

  前面介绍了查询的基础扩展,下面准备给大家介绍一些有用的查询封装手法,比如对日期范围查询,数值范围查询的封装等,为了支持这些功能,需要增强公共操作类。

  Lambda表达式公共操作类,我在前面已经简单介绍过,要么从Lambda表达式中获取信息,要么动态创建Lambda表达式,本篇直接贴代码,如果发现有些代码与以前不同,原因有二,一是之前还未用到,只发了部分代码,二是可能代码已重构。需要说明的一点是,我不会考虑代码兼容性,API命名随时可能修改,如果你直接将本系列的代码用到你的项目上要格外注意。

  修改Util项目的Lambda类,代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using DynamicExpression = Util.Lambdas.Dynamics.DynamicExpression; namespace Util {
/// <summary>
/// Lambda表达式操作
/// </summary>
public class Lambda { #region GetName(获取成员名称) /// <summary>
/// 获取成员名称,范例:t => t.Name,返回 Name
/// </summary>
/// <param name="expression">表达式,范例:t => t.Name</param>
public static string GetName( LambdaExpression expression ) {
var memberExpression = GetMemberExpression( expression );
if ( memberExpression == null )
return string.Empty;
string result = memberExpression.ToString();
return result.Substring( result.IndexOf( ".", StringComparison.Ordinal ) + );
} /// <summary>
/// 获取成员表达式
/// </summary>
private static MemberExpression GetMemberExpression( LambdaExpression expression ) {
if ( expression == null )
return null;
var unaryExpression = expression.Body as UnaryExpression;
if ( unaryExpression == null )
return expression.Body as MemberExpression;
return unaryExpression.Operand as MemberExpression;
} #endregion #region GetMember(获取成员) /// <summary>
/// 获取成员
/// </summary>
/// <param name="expression">表达式,范例:t => t.Name</param>
public static MemberInfo GetMember( LambdaExpression expression ) {
var memberExpression = GetMemberExpression( expression );
if ( memberExpression == null )
return null;
return memberExpression.Member;
} #endregion #region GetValue(获取值) /// <summary>
/// 获取值,范例:t => t.Name == "A",返回 A
/// </summary>
/// <param name="expression">表达式,范例:t => t.Name == "A"</param>
public static object GetValue( LambdaExpression expression ) {
if ( expression == null )
return null;
var memberExpression = expression.Body as MemberExpression;
if ( memberExpression != null )
return GetMemberValue( memberExpression );
BinaryExpression binaryExpression = GetBinaryExpression( expression );
if ( binaryExpression != null )
return GetBinaryValue( binaryExpression );
var callExpression = expression.Body as MethodCallExpression;
if ( callExpression != null )
return GetMethodValue( callExpression );
return null;
} /// <summary>
/// 获取二元表达式
/// </summary>
private static BinaryExpression GetBinaryExpression( LambdaExpression expression ) {
var binaryExpression = expression.Body as BinaryExpression;
if ( binaryExpression != null )
return binaryExpression;
var unaryExpression = expression.Body as UnaryExpression;
if ( unaryExpression == null )
return null;
return unaryExpression.Operand as BinaryExpression;
} /// <summary>
/// 获取二元表达式的值
/// </summary>
private static object GetBinaryValue( BinaryExpression binaryExpression ) {
var unaryExpression = binaryExpression.Right as UnaryExpression;
if ( unaryExpression != null )
return GetConstantValue( unaryExpression.Operand );
var memberExpression = binaryExpression.Right as MemberExpression;
if ( memberExpression != null )
return GetMemberValue( memberExpression );
return GetConstantValue( binaryExpression.Right );
} /// <summary>
/// 获取属性表达式的值
/// </summary>
private static object GetMemberValue( MemberExpression expression ) {
if ( expression == null )
return null;
var field = expression.Member as FieldInfo;
if ( field != null ) {
var constValue = GetConstantValue( expression.Expression );
return field.GetValue( constValue );
}
var property = expression.Member as PropertyInfo;
if ( property == null )
return null;
var value = GetMemberValue( expression.Expression as MemberExpression );
return property.GetValue( value );
} /// <summary>
/// 获取常量值
/// </summary>
private static object GetConstantValue( Expression expression ) {
var constantExpression = expression as ConstantExpression;
if ( constantExpression == null )
return null;
return constantExpression.Value;
} /// <summary>
/// 获取方法调用表达式的值
/// </summary>
private static object GetMethodValue( MethodCallExpression callExpression ) {
var argumentExpression = callExpression.Arguments.FirstOrDefault();
var memberExpression = argumentExpression as MemberExpression;
if ( memberExpression != null )
return GetMemberValue( memberExpression );
return GetConstantValue( argumentExpression );
} #endregion #region GetParameter(获取参数) /// <summary>
/// 获取参数,范例:t.Name,返回 t
/// </summary>
/// <param name="expression">表达式,范例:t.Name</param>
public static ParameterExpression GetParameter( LambdaExpression expression ) {
if ( expression == null )
return null;
BinaryExpression binaryExpression = GetBinaryExpression( expression );
if ( binaryExpression == null )
return null;
return GetParameterByMember( binaryExpression.Left );
} /// <summary>
/// 递归获取参数
/// </summary>
private static ParameterExpression GetParameterByMember( Expression expression ) {
if ( expression == null )
return null;
ParameterExpression result = expression as ParameterExpression;
if ( result != null )
return result;
MemberExpression memberExpression = expression as MemberExpression;
if ( memberExpression == null )
return null;
return GetParameterByMember( memberExpression.Expression );
} #endregion #region GetAttribute(获取特性) /// <summary>
/// 获取特性
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <typeparam name="TProperty">属性类型</typeparam>
/// <typeparam name="TAttribute">特性类型</typeparam>
/// <param name="propertyExpression">属性表达式</param>
public static TAttribute GetAttribute<TEntity, TProperty, TAttribute>( Expression<Func<TEntity, TProperty>> propertyExpression )
where TAttribute : Attribute {
var memberInfo = GetMember( propertyExpression );
return memberInfo.GetCustomAttribute<TAttribute>();
} #endregion #region GetAttributes(获取特性列表) /// <summary>
/// 获取特性列表
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <typeparam name="TProperty">属性类型</typeparam>
/// <typeparam name="TAttribute">特性类型</typeparam>
/// <param name="propertyExpression">属性表达式</param>
public static IEnumerable<TAttribute> GetAttributes<TEntity, TProperty, TAttribute>( Expression<Func<TEntity, TProperty>> propertyExpression ) where TAttribute : Attribute {
var memberInfo = GetMember( propertyExpression );
return memberInfo.GetCustomAttributes<TAttribute>();
} #endregion #region Constant(获取常量) /// <summary>
/// 获取常量表达式,自动转换值的类型
/// </summary>
/// <param name="expression">表达式</param>
/// <param name="value">值</param>
public static ConstantExpression Constant( Expression expression, object value ) {
var memberExpression = expression as MemberExpression;
if ( memberExpression == null )
return Expression.Constant( value );
return Expression.Constant( value, memberExpression.Type );
} #endregion #region GetCriteriaCount(获取谓词条件的个数) /// <summary>
/// 获取谓词条件的个数
/// </summary>
/// <param name="expression">谓词表达式,范例:t => t.Name == "A"</param>
public static int GetCriteriaCount( LambdaExpression expression ) {
if ( expression == null )
return ;
var result = expression.ToString().Replace( "AndAlso", "|" ).Replace( "OrElse", "|" );
return result.Split( '|' ).Count();
} #endregion #region Equal(等于表达式) /// <summary>
/// 创建等于运算lambda表达式
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="propertyName">属性名</param>
/// <param name="value">值</param>
public static Expression<Func<T, bool>> Equal<T>( string propertyName, object value ) {
var parameter = CreateParameter<T>();
return parameter.Property( propertyName )
.Equal( value )
.ToLambda<Func<T, bool>>( parameter );
} /// <summary>
/// 创建参数
/// </summary>
private static ParameterExpression CreateParameter<T>() {
return Expression.Parameter( typeof( T ), "t" );
} #endregion #region NotEqual(不等于表达式) /// <summary>
/// 创建不等于运算lambda表达式
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="propertyName">属性名</param>
/// <param name="value">值</param>
public static Expression<Func<T, bool>> NotEqual<T>( string propertyName, object value ) {
var parameter = CreateParameter<T>();
return parameter.Property( propertyName )
.NotEqual( value )
.ToLambda<Func<T, bool>>( parameter );
} #endregion #region Greater(大于表达式) /// <summary>
/// 创建大于运算lambda表达式
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="propertyName">属性名</param>
/// <param name="value">值</param>
public static Expression<Func<T, bool>> Greater<T>( string propertyName, object value ) {
var parameter = CreateParameter<T>();
return parameter.Property( propertyName )
.Greater( value )
.ToLambda<Func<T, bool>>( parameter );
} #endregion #region Less(小于表达式) /// <summary>
/// 创建小于运算lambda表达式
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="propertyName">属性名</param>
/// <param name="value">值</param>
public static Expression<Func<T, bool>> Less<T>( string propertyName, object value ) {
var parameter = CreateParameter<T>();
return parameter.Property( propertyName )
.Less( value )
.ToLambda<Func<T, bool>>( parameter );
} #endregion #region GreaterEqual(大于等于表达式) /// <summary>
/// 创建大于等于运算lambda表达式
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="propertyName">属性名</param>
/// <param name="value">值</param>
public static Expression<Func<T, bool>> GreaterEqual<T>( string propertyName, object value ) {
var parameter = CreateParameter<T>();
return parameter.Property( propertyName )
.GreaterEqual( value )
.ToLambda<Func<T, bool>>( parameter );
} #endregion #region LessEqual(小于等于表达式) /// <summary>
/// 创建小于等于运算lambda表达式
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="propertyName">属性名</param>
/// <param name="value">值</param>
public static Expression<Func<T, bool>> LessEqual<T>( string propertyName, object value ) {
var parameter = CreateParameter<T>();
return parameter.Property( propertyName )
.LessEqual( value )
.ToLambda<Func<T, bool>>( parameter );
} #endregion #region Contains(调用Contains方法) /// <summary>
/// 调用Contains方法
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="propertyName">属性名</param>
/// <param name="value">值</param>
public static Expression<Func<T, bool>> Contains<T>( string propertyName, object value ) {
return Call<T>( propertyName, "Contains", value );
} /// <summary>
/// 调用方法
/// </summary>
private static Expression<Func<T, bool>> Call<T>( string propertyName, string methodName, object value ) {
var parameter = CreateParameter<T>();
return parameter.Property( propertyName )
.Call( methodName, value )
.ToLambda<Func<T, bool>>( parameter );
} #endregion #region Starts(调用StartsWith方法) /// <summary>
/// 调用StartsWith方法
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="propertyName">属性名</param>
/// <param name="value">值</param>
public static Expression<Func<T, bool>> Starts<T>( string propertyName, string value ) {
var parameter = CreateParameter<T>();
var property = parameter.Property( propertyName );
var call = Expression.Call( property, property.Type.GetMethod( "StartsWith", new Type[] { typeof( string ) } ),
Expression.Constant( value ) );
return call.ToLambda<Func<T, bool>>( parameter );
} #endregion #region Ends(调用EndsWith方法) /// <summary>
/// 调用EndsWith方法
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="propertyName">属性名</param>
/// <param name="value">值</param>
public static Expression<Func<T, bool>> Ends<T>( string propertyName, string value ) {
var parameter = CreateParameter<T>();
var property = parameter.Property( propertyName );
var call = Expression.Call( property, property.Type.GetMethod( "EndsWith", new Type[] { typeof( string ) } ),
Expression.Constant( value ) );
return call.ToLambda<Func<T, bool>>( parameter );
} #endregion #region ParsePredicate(解析为谓词表达式) /// <summary>
/// 解析为谓词表达式
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="propertyName">属性名</param>
/// <param name="value">值</param>
/// <param name="operator">运算符</param>
public static Expression<Func<T, bool>> ParsePredicate<T>( string propertyName, object value, Operator @operator ) {
var parameter = Expression.Parameter( typeof( T ), "t" );
return parameter.Property( propertyName ).Operation( @operator, value ).ToLambda<Func<T, bool>>( parameter );
} /// <summary>
/// 解析为谓词表达式
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="predicateExpression">谓词表达式字符串,参数占位符为@0,@1,@2 ...</param>
/// <param name="values">值</param>
public static Expression<Func<T, bool>> ParsePredicate<T>( string predicateExpression, params object[] values ) {
return DynamicExpression.ParseLambda( typeof( T ), typeof( bool ), predicateExpression, values ) as Expression<Func<T, bool>>;
} #endregion
}
}

  在Util.Tests单元测试项目新建两个测试样例,代码如下。

using System.ComponentModel.DataAnnotations;

namespace Util.Tests.Samples {
/// <summary>
/// 测试1
/// </summary>
public class Test1 {
[StringLength( , ErrorMessage = "长度不能超过20" )]
[Required( ErrorMessage = "名称不能为空" )]
[Display( Name = "名称" )]
public string Name { get; set; }
public int Age { get; set; }
public int? NullableInt { get; set; }
public decimal? NullableDecimal { get; set; }
public TestA A { get; set; }
public class TestA {
public int Integer { get; set; }
public string Address { get; set; }
public TestB B { get; set; }
public class TestB {
public string Name { get; set; }
}
}
}
} using System;
using System.ComponentModel.DataAnnotations; namespace Util.Tests.Samples {
/// <summary>
/// 测试2
/// </summary>
[Serializable]
public class Test2 {
public Test2() {
}
public Test2( string name ) {
Name = name;
} [Required(ErrorMessage = "名称不能为空")]
public string Name { get; set; }
public int Int { get; set; }
public int? NullableInt { get; set; }
public decimal? NullableDecimal { get; set; }
public decimal Decimal { get; set; }
public TestA A { get; set; }
[Serializable]
public class TestA {
public int Integer { get; set; }
public string Address { get; set; }
public TestB B { get; set; }
public class TestB {
public string Name { get; set; }
}
}
}
}

  修改Util.Tests单元测试项目LambdaTest单元测试,代码如下。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Util.Tests.Samples; namespace Util.Tests {
/// <summary>
/// 表达式测试
/// </summary>
[TestClass]
public class LambdaTest { #region GetName(获取成员名称) /// <summary>
/// 获取成员名称
/// </summary>
[TestMethod]
public void TestGetName() {
//空值返回空字符串
Assert.AreEqual( "", Lambda.GetName( null ) ); //返回一级属性名
Expression<Func<Test1, string>> expression = test => test.Name;
Assert.AreEqual( "Name", Lambda.GetName( expression ) ); //返回二级属性名
Expression<Func<Test1, string>> expression2 = test => test.A.Address;
Assert.AreEqual( "A.Address", Lambda.GetName( expression2 ) ); //返回三级属性名
Expression<Func<Test1, string>> expression3 = test => test.A.B.Name;
Assert.AreEqual( "A.B.Name", Lambda.GetName( expression3 ) ); //测试可空整型
Expression<Func<Test1, int?>> expression4 = test => test.NullableInt;
Assert.AreEqual( "NullableInt", Lambda.GetName( expression4 ) ); //测试类型转换
Expression<Func<Test1, int?>> expression5 = test => test.Age;
Assert.AreEqual( "Age", Lambda.GetName( expression5 ) );
} #endregion #region GetValue(获取成员值) /// <summary>
/// 获取成员值,委托返回类型为Object
/// </summary>
[TestMethod]
public void TestGetValue_Object() {
Expression<Func<Test1, object>> expression = test => test.Name == "A";
Assert.AreEqual( "A", Lambda.GetValue( expression ) );
} /// <summary>
/// 获取成员值,委托返回类型为bool
/// </summary>
[TestMethod]
public void TestGetValue_Boolean() {
//空值返回null
Assert.AreEqual( null, Lambda.GetValue( null ) ); //一级返回值
Expression<Func<Test1, bool>> expression = test => test.Name == "A";
Assert.AreEqual( "A", Lambda.GetValue( expression ) ); //二级返回值
Expression<Func<Test1, bool>> expression2 = test => test.A.Integer == ;
Assert.AreEqual( , Lambda.GetValue( expression2 ) ); //三级返回值
Expression<Func<Test1, bool>> expression3 = test => test.A.B.Name == "B";
Assert.AreEqual( "B", Lambda.GetValue( expression3 ) );
} /// <summary>
/// 获取可空类型的值
/// </summary>
[TestMethod]
public void TestGetValue_Nullable() {
//可空整型
Expression<Func<Test1, bool>> expression = test => test.NullableInt == ;
Assert.AreEqual( , Lambda.GetValue( expression ) ); //可空decimal
expression = test => test.NullableDecimal == 1.5M;
Assert.AreEqual( 1.5M, Lambda.GetValue( expression ) );
} /// <summary>
/// 获取成员值,运算符为方法
/// </summary>
[TestMethod]
public void TestGetValue_Method() {
//1级返回值
Expression<Func<Test1, bool>> expression = t => t.Name.Contains( "A" );
Assert.AreEqual( "A", Lambda.GetValue( expression ) ); //二级返回值
expression = t => t.A.Address.Contains( "B" );
Assert.AreEqual( "B", Lambda.GetValue( expression ) ); //三级返回值
expression = t => t.A.B.Name.StartsWith( "C" );
Assert.AreEqual( "C", Lambda.GetValue( expression ) );
} /// <summary>
/// 从实例中获取值
/// </summary>
[TestMethod]
public void TestGetValue_Instance() {
var test = new Test1() { Name = "a", A = new Test1.TestA() { Address = "b", B = new Test1.TestA.TestB() { Name = "c" } } }; //一级属性
Expression<Func<string>> expression = () => test.Name;
Assert.AreEqual( "a", Lambda.GetValue( expression ) ); //二级属性
Expression<Func<string>> expression2 = () => test.A.Address;
Assert.AreEqual( "b", Lambda.GetValue( expression2 ) ); //三级属性
Expression<Func<string>> expression3 = () => test.A.B.Name;
Assert.AreEqual( "c", Lambda.GetValue( expression3 ) );
} /// <summary>
/// 测试值为复杂类型
/// </summary>
[TestMethod]
public void TestGetValue_Complex() {
var test = new Test1() { Name = "a", A = new Test1.TestA() { Address = "b" } }; //获取表达式的值
Expression<Func<Test1, bool>> expression = t => t.Name == test.Name;
Assert.AreEqual( "a", Lambda.GetValue( expression ), "==test.Name" );
Expression<Func<Test1, bool>> expression2 = t => t.Name == test.A.Address;
Assert.AreEqual( "b", Lambda.GetValue( expression2 ), "==test.A.Address" ); //获取方法的值
Expression<Func<Test1, bool>> expression3 = t => t.Name.Contains( test.Name );
Assert.AreEqual( "a", Lambda.GetValue( expression3 ), "Contains test.Name" );
Expression<Func<Test1, bool>> expression4 = t => t.Name.Contains( test.A.Address );
Assert.AreEqual( "b", Lambda.GetValue( expression4 ), "==test.A.Address" );
} #endregion #region GetParameter(获取参数) /// <summary>
/// 获取参数
/// </summary>
[TestMethod]
public void TestGetParameter() {
//空值返回null
Assert.AreEqual( null, Lambda.GetParameter( null ) ); //一级返回值
Expression<Func<Test1, object>> expression = test => test.Name == "A";
Assert.AreEqual( "test", Lambda.GetParameter( expression ).ToString() ); //二级返回值
Expression<Func<Test1, object>> expression2 = test => test.A.Integer == ;
Assert.AreEqual( "test", Lambda.GetParameter( expression2 ).ToString() ); //三级返回值
Expression<Func<Test1, object>> expression3 = test => test.A.B.Name == "B";
Assert.AreEqual( "test", Lambda.GetParameter( expression3 ).ToString() );
} #endregion #region GetCriteriaCount(获取谓词条件的个数) /// <summary>
/// 获取谓词条件的个数
/// </summary>
[TestMethod]
public void TestGetCriteriaCount() {
//0个条件
Assert.AreEqual( , Lambda.GetCriteriaCount( null ) ); //1个条件
Expression<Func<Test1, bool>> expression = test => test.Name == "A";
Assert.AreEqual( , Lambda.GetCriteriaCount( expression ) ); //2个条件,与连接符
expression = test => test.Name == "A" && test.Name == "B";
Assert.AreEqual( , Lambda.GetCriteriaCount( expression ) ); //2个条件,或连接符
expression = test => test.Name == "A" || test.Name == "B";
Assert.AreEqual( , Lambda.GetCriteriaCount( expression ) ); //3个条件
expression = test => test.Name == "A" && test.Name == "B" || test.Name == "C";
Assert.AreEqual( , Lambda.GetCriteriaCount( expression ) ); //3个条件,包括导航属性
expression = test => test.A.Address == "A" && test.Name == "B" || test.Name == "C";
Assert.AreEqual( , Lambda.GetCriteriaCount( expression ) );
} /// <summary>
/// 获取谓词条件的个数,运算符为方法
/// </summary>
[TestMethod]
public void TestGetCriteriaCount_Method() {
//1个条件
Expression<Func<Test1, bool>> expression = t => t.Name.Contains( "A" );
Assert.AreEqual( , Lambda.GetCriteriaCount( expression ) ); //2个条件,与连接
expression = t => t.Name.Contains( "A" ) && t.Name == "A";
Assert.AreEqual( , Lambda.GetCriteriaCount( expression ) ); //2个条件,或连接,包含导航属性
expression = t => t.Name.Contains( "A" ) || t.A.Address == "A";
Assert.AreEqual( , Lambda.GetCriteriaCount( expression ) );
} #endregion #region Equal(创建等于表达式) /// <summary>
/// 创建等于表达式
/// </summary>
[TestMethod]
public void TestEqual() {
//一级属性
Expression<Func<Test1, bool>> expected = t => t.Age == ;
Assert.AreEqual( expected.ToString(), Lambda.Equal<Test1>( "Age", ).ToString() ); //二级属性
Expression<Func<Test1, bool>> expected2 = t => t.A.Integer == ;
Assert.AreEqual( expected2.ToString(), Lambda.Equal<Test1>( "A.Integer", ).ToString() );
} #endregion #region NotEqual(创建不等于表达式) /// <summary>
/// 创建不等于表达式
/// </summary>
[TestMethod]
public void TestNotEqual() {
//一级属性
Expression<Func<Test1, bool>> expected = t => t.Age != ;
Assert.AreEqual( expected.ToString(), Lambda.NotEqual<Test1>( "Age", ).ToString() ); //二级属性
Expression<Func<Test1, bool>> expected2 = t => t.A.Integer != ;
Assert.AreEqual( expected2.ToString(), Lambda.NotEqual<Test1>( "A.Integer", ).ToString() );
} #endregion #region Greater(创建大于表达式) /// <summary>
/// 创建大于表达式
/// </summary>
[TestMethod]
public void TestGreater() {
//一级属性
Expression<Func<Test1, bool>> expected = t => t.Age > ;
Assert.AreEqual( expected.ToString(), Lambda.Greater<Test1>( "Age", ).ToString() ); //二级属性
Expression<Func<Test1, bool>> expected2 = t => t.A.Integer > ;
Assert.AreEqual( expected2.ToString(), Lambda.Greater<Test1>( "A.Integer", ).ToString() );
} #endregion #region Less(创建小于表达式) /// <summary>
/// 创建小于表达式
/// </summary>
[TestMethod]
public void TestLess() {
//一级属性
Expression<Func<Test1, bool>> expected = t => t.Age < ;
Assert.AreEqual( expected.ToString(), Lambda.Less<Test1>( "Age", ).ToString() ); //二级属性
Expression<Func<Test1, bool>> expected2 = t => t.A.Integer < ;
Assert.AreEqual( expected2.ToString(), Lambda.Less<Test1>( "A.Integer", ).ToString() );
} #endregion #region GreaterEqual(创建大于等于表达式) /// <summary>
/// 创建大于等于表达式
/// </summary>
[TestMethod]
public void TestGreaterEqual() {
//一级属性
Expression<Func<Test1, bool>> expected = t => t.Age >= ;
Assert.AreEqual( expected.ToString(), Lambda.GreaterEqual<Test1>( "Age", ).ToString() ); //二级属性
Expression<Func<Test1, bool>> expected2 = t => t.A.Integer >= ;
Assert.AreEqual( expected2.ToString(), Lambda.GreaterEqual<Test1>( "A.Integer", ).ToString() );
} #endregion #region LessEqual(创建小于等于表达式) /// <summary>
/// 创建小于等于表达式
/// </summary>
[TestMethod]
public void TestLessEqual() {
//一级属性
Expression<Func<Test1, bool>> expected = t => t.Age <= ;
Assert.AreEqual( expected.ToString(), Lambda.LessEqual<Test1>( "Age", ).ToString() ); //二级属性
Expression<Func<Test1, bool>> expected2 = t => t.A.Integer <= ;
Assert.AreEqual( expected2.ToString(), Lambda.LessEqual<Test1>( "A.Integer", ).ToString() );
} #endregion #region Contains(调用Contains方法) /// <summary>
/// 调用Contains方法
/// </summary>
[TestMethod]
public void TestContains() {
//一级属性
Expression<Func<Test1, bool>> expected = t => t.Name.Contains( "A" );
Assert.AreEqual( expected.ToString(), Lambda.Contains<Test1>( "Name", "A" ).ToString() ); //二级属性
Expression<Func<Test1, bool>> expected2 = t => t.A.Address.Contains( "A" );
Assert.AreEqual( expected2.ToString(), Lambda.Contains<Test1>( "A.Address", "A" ).ToString() ); //三级属性
Expression<Func<Test1, bool>> expected3 = t => t.A.B.Name.Contains( "A" );
Assert.AreEqual( expected3.ToString(), Lambda.Contains<Test1>( "A.B.Name", "A" ).ToString() );
} #endregion #region Starts(调用StartsWith方法) /// <summary>
/// 调用StartsWith方法
/// </summary>
[TestMethod]
public void TestStarts() {
//一级属性
Expression<Func<Test1, bool>> expected = t => t.Name.StartsWith( "A" );
Assert.AreEqual( expected.ToString(), Lambda.Starts<Test1>( "Name", "A" ).ToString() ); //二级属性
Expression<Func<Test1, bool>> expected2 = t => t.A.Address.StartsWith( "A" );
Assert.AreEqual( expected2.ToString(), Lambda.Starts<Test1>( "A.Address", "A" ).ToString() ); //三级属性
Expression<Func<Test1, bool>> expected3 = t => t.A.B.Name.StartsWith( "A" );
Assert.AreEqual( expected3.ToString(), Lambda.Starts<Test1>( "A.B.Name", "A" ).ToString() );
} #endregion #region Ends(调用EndsWith方法) /// <summary>
/// 调用EndsWith方法
/// </summary>
[TestMethod]
public void TestEnds() {
//一级属性
Expression<Func<Test1, bool>> expected = t => t.Name.EndsWith( "A" );
Assert.AreEqual( expected.ToString(), Lambda.Ends<Test1>( "Name", "A" ).ToString() ); //二级属性
Expression<Func<Test1, bool>> expected2 = t => t.A.Address.EndsWith( "A" );
Assert.AreEqual( expected2.ToString(), Lambda.Ends<Test1>( "A.Address", "A" ).ToString() ); //三级属性
Expression<Func<Test1, bool>> expected3 = t => t.A.B.Name.EndsWith( "A" );
Assert.AreEqual( expected3.ToString(), Lambda.Ends<Test1>( "A.B.Name", "A" ).ToString() );
} #endregion #region GetConst(获取常量表达式) /// <summary>
/// 获取常量表达式
/// </summary>
[TestMethod]
public void TestGetConst() {
Expression<Func<Test1, int?>> property = t => t.NullableInt;
ConstantExpression constantExpression = Lambda.Constant( property, );
Assert.AreEqual( typeof( int ), constantExpression.Type );
} #endregion #region GetAttribute(获取特性) /// <summary>
/// 测试获取特性
/// </summary>
[TestMethod]
public void TestGetAttribute() {
DisplayAttribute attribute = Lambda.GetAttribute<Test1, string, DisplayAttribute>( t => t.Name );
Assert.AreEqual( "名称", attribute.Name );
} #endregion #region GetAttributes(获取特性列表) /// <summary>
/// 测试获取特性列表
/// </summary>
[TestMethod]
public void TestGetAttributes() {
IEnumerable<ValidationAttribute> attributes = Lambda.GetAttributes<Test1, string, ValidationAttribute>( t => t.Name );
Assert.AreEqual( , attributes.Count() );
} #endregion
}
}

  这些操作方法的作用,大家自己看下测试就明白了,具体应用在后面介绍。

  .Net应用程序框架交流QQ群: 386092459,欢迎有兴趣的朋友加入讨论。

  谢谢大家的持续关注,我的博客地址:http://www.cnblogs.com/xiadao521/

  下载地址:http://files.cnblogs.com/xiadao521/Util.2015.1.6.1.rar

Util应用程序框架公共操作类(八):Lambda表达式公共操作类(二)的更多相关文章

  1. Util应用程序框架公共操作类&lpar;七&rpar;&colon;Lambda表达式公共操作类

    前一篇扩展了两个常用验证方法,本文将封装两个Lambda表达式操作,用来为下一篇的查询扩展服务. Lambda表达式是一种简洁的匿名函数语法,可以用它将方法作为委托参数传递.在Linq中,大量使用La ...

  2. Util应用程序框架公共操作类&lpar;十二&rpar;&colon;Lambda表达式公共操作类&lpar;三&rpar;

    今天在开发一个简单查询时,发现我的Lambda操作类的GetValue方法无法正确获取枚举类型值,以至查询结果错误. 我增加了几个单元测试来捕获错误,代码如下. /// <summary> ...

  3. Util应用程序框架公共操作类&lpar;九&rpar;&colon;Lambda表达式扩展

    上一篇对Lambda表达式公共操作类进行了一些增强,本篇使用扩展方法对Lambda表达式进行扩展. 修改Util项目的Extensions.Expression.cs文件,代码如下. using Sy ...

  4. Util应用程序框架公共操作类&lpar;六&rpar;&colon;验证扩展

    前面介绍了仓储的基本操作,下面准备开始扩展查询,在扩展查询之前,首先要增加两个公共操作类,一个是经常要用到的验证方法,另一个是Lambda表达式的操作类. 很多时候,我们会判断一个对象是否为null, ...

  5. Util应用程序框架公共操作类&lpar;四&rpar;&colon;验证公共操作类

    为了能够验证领域实体,需要一个验证公共操作类来提供支持.由于我将使用企业库(Enterprise Library)的验证组件来完成这项任务,所以本文也将演示对第三方框架的封装要点. .Net提供了一个 ...

  6. Util应用程序框架公共操作类

    随笔分类 - Util应用程序框架公共操作类 Util应用程序框架公共操作类 Util应用程序框架公共操作类(五):异常公共操作类 摘要: 任何系统都需要处理错误,本文介绍的异常公共操作类,用于对业务 ...

  7. Effective Java 第三版——42&period;lambda表达式优于匿名类

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. Java 终于有 Lambda 表达式啦~Java 8 语言变化&mdash&semi;&mdash&semi;Lambda 表达式和接口类更改【转载】

    原文地址 en cn 下载 Demo Java™ 8 包含一些重要的新的语言功能,为您提供了构建程序的更简单方式.Lambda 表达式 为内联代码块定义一种新语法,其灵活性与匿名内部类一样,但样板文件 ...

  9. JAVA8 in Action:行为参数化,匿名类及lambda表达式的初步认知实例整理

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.functio ...

随机推荐

  1. Android中使用ShareSDK集成分享功能

    引言      现在APP开发集成分享功能已经是非常普遍的需求了.其他集成分享技术我没有使用过,今天我就来介绍下使用ShareSDK来进行分享功能开发的一些基本步骤和注意点,帮助朋友们避免一些坑.好了 ...

  2. Android 四大组件 与 MVC 架构模式

    作为一个刚从JAVA转过来的Android程序员总会思考android MVC是什么样的? 首先,我们必须得说Android追寻着MVC架构,那就得先说一下MVC是个啥东西! 总体而来说MVC不能说是 ...

  3. 【ASP&period;NET 基础】ASP&period;NET内置对象

    准确地说,asp.net 并没有内置对象这一说,jsp 里确实把 request.response 这些当作 jsp 的内置对象,这里只不过是借用了一下 jsp 的说法而已.在 Web 中处于中心的是 ...

  4. Running Kafka At Scale

    https://engineering.linkedin.com/kafka/running-kafka-scale If data is the lifeblood of high technolo ...

  5. WPF工作笔记:本地化支持、主进程通知、两种最常用异步编程方式

    1.本地化支持 (1)重写控件默认的依赖属性LanguageProperty FrameworkElement.LanguageProperty.OverrideMetadata( typeof(Fr ...

  6. nvm 知识点

    事项 作用 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash 安装nvm comm ...

  7. MySQL优化--INSERT ON DUPLICATE UPDATE死锁

    INSERT ON DUPLICATE UPDATE与死锁 在MySQL中提供两种插入更新的方式:REPLACE INTO和INSERT ON DUPLICATE UPDATE,简化了“存在则更新,不 ...

  8. 基于jQuery-ui实现多滑块slider

    效果图: 代码: <!doctype html> <html lang="en"> <head> <meta charset=" ...

  9. 手机nv

    NV值是记录手机的射频参数的,和手机的IMEI号.手机信号.WIFI信号等有关,如果NV值刷没了,手机没有这些校准的数据了,会对手机有一定的影响. qcn里面包含手机的imei 所有改变imei就改变 ...

  10. Unity3D Shader 马赛克后期效果

    //效果图 //Shader代码 Shader "Hidden/Mosaic" { Properties { _MainTex ("Texture", 2D) ...