Exemplo n.º 1
0
 private static Expression <Func <TEntity, bool> > GetContainExpression <TEntity>(ParameterExpression param, Lambda lambda, MemberExpression member)
 {
     if (lambda.Value is IEnumerable)
     {
         var convertValue = HandyConvert.ToList(lambda.Value as IEnumerable, lambda.PropertyType);
         var methodInfo   = typeof(List <>).MakeGenericType(lambda.PropertyType).GetMethod("Contains");
         var body         = Expression.Call(Expression.Constant(convertValue), methodInfo, member);
         return(Expression.Lambda <Func <TEntity, bool> >(body, param));
     }
     else
     {
         throw new ArgumentException("Contain 只适用与数组、集合等容器");
     }
 }
Exemplo n.º 2
0
        public void TestToList()
        {
            Assert.IsNull(HandyConvert.ToList <string>(null));
            Assert.IsTrue(HandyConvert.ToList <string>(new string[] { "", "dd" }).Count == 2);
            Assert.IsTrue(HandyConvert.ToList <int>(new string[] { "456", "123" }).Count == 2);

            Assert.IsNotNull(HandyConvert.ToList(new string[] { "", "dd" }, typeof(string)) as List <string>);
            Assert.IsNotNull(HandyConvert.ToList(new HandyConvertTest_A[]
            {
                new HandyConvertTest_A
                {
                    a = "aa"
                },
                new HandyConvertTest_A
                {
                    a = "bb"
                }
            }, typeof(HandyConvertTest_A)) as List <dynamic>);
        }