コード例 #1
0
        public void TestGetValue_DateTimeNow()
        {
            Expression <Func <Sample, bool> > expression = t => t.DateValue == DateTime.Now;
            var value = Lambdas.GetValue(expression);

            Assert.NotNull(Conv.ToDateOrNull(value));
        }
コード例 #2
0
        public void TestGetValue_Instance()
        {
            var test = new Sample()
            {
                StringValue = "a", BoolValue = true, Test2 = new Sample2()
                {
                    StringValue = "b", Test3 = new Sample3()
                    {
                        StringValue = "c"
                    }
                }
            };

            Expression <Func <string> > expression = () => test.StringValue;

            Assert.Equal("a", Lambdas.GetValue(expression));

            Expression <Func <string> > expression2 = () => test.Test2.StringValue;

            Assert.Equal("b", Lambdas.GetValue(expression2));

            Expression <Func <string> > expression3 = () => test.Test2.Test3.StringValue;

            Assert.Equal("c", Lambdas.GetValue(expression3));

            Expression <Func <bool> > expression4 = () => test.BoolValue;

            Assert.True(Conv.ToBool(Lambdas.GetValue(expression4)));
        }
コード例 #3
0
        public void TestGetValue()
        {
            Assert.Null(Lambdas.GetValue(null));

            Expression <Func <Sample, bool> > expression = t => t.StringValue == "A";

            Assert.Equal("A", Lambdas.GetValue(expression));

            Expression <Func <Sample, bool> > expression2 = t => t.Test2.IntValue == 1;

            Assert.Equal(1, Lambdas.GetValue(expression2));

            Expression <Func <Sample, bool> > expression3 = t => t.Test2.Test3.StringValue == "B";

            Assert.Equal("B", Lambdas.GetValue(expression3));

            var value = Guid.NewGuid();
            Expression <Func <Sample, bool> > expression4 = t => t.GuidValue == value;

            Assert.Equal(value, Lambdas.GetValue(expression4));

            Expression <Func <Sample, bool> > expression5 = t => 1 == t.Test2.IntValue;

            Assert.Equal(1, Lambdas.GetValue(expression5));
        }
コード例 #4
0
        private object GetGuidValue(Guid id)
        {
            Expression <Func <Sample, bool> > expression = t => t.GuidValue == id;
            var value = Lambdas.GetValue(expression);

            return(value);
        }
コード例 #5
0
 /// <summary>
 /// Or连接条件
 /// </summary>
 /// <typeparam name="TEntity">实体类型</typeparam>
 /// <param name="conditions">查询条件</param>
 public void OrIfNotEmpty <TEntity>(params Expression <Func <TEntity, bool> >[] conditions)
 {
     if (conditions == null)
     {
         return;
     }
     foreach (var condition in conditions)
     {
         if (condition == null)
         {
             continue;
         }
         if (Lambdas.GetConditionCount(condition) > 1)
         {
             throw new InvalidOperationException(string.Format(LibraryResource.OnlyOnePredicate, condition));
         }
         if (string.IsNullOrWhiteSpace(Lambdas.GetValue(condition).SafeString()))
         {
             continue;
         }
         var predicate = _expressionResolver.Resolve(condition);
         if (predicate == null)
         {
             continue;
         }
         Or(predicate);
     }
 }
コード例 #6
0
        public void TestGetValue_NewGuid()
        {
            Expression <Func <Sample, bool> > expression = t => t.GuidValue == Guid.NewGuid();
            var value = Lambdas.GetValue(expression);

            Assert.NotEqual(Guid.Empty, Conv.ToGuid(value));
        }
コード例 #7
0
        /// <summary>
        /// Add
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="newValue"></param>
        /// <typeparam name="TObject"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        public void Add <TObject, TProperty, TValue>(Expression <Func <TObject, TProperty> > expression, TValue newValue)
            where TObject : IDomainObject
        {
            var name  = Lambdas.GetName(expression);
            var desc  = Reflections.GetDescription(Lambdas.GetMember(expression));
            var value = Lambdas.GetValue(expression);

            Add(name, desc, value.CastTo <TValue>(), newValue);
        }
コード例 #8
0
        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="TObject">领域对象类型</typeparam>
        /// <typeparam name="TProperty">属性类型</typeparam>
        /// <typeparam name="TValue">值类型</typeparam>
        /// <param name="expression">属性表达式。范例:t => t.Name</param>
        /// <param name="newValue">新值。范例:newEntity.Name</param>
        public void Add <TObject, TProperty, TValue>(Expression <Func <TObject, TProperty> > expression, TValue newValue) where TObject : IDomainObject
        {
            var member = Lambdas.GetMemberExpression(expression);
            var name   = Lambdas.GetMemberName(member);
            var desc   = Reflection.Reflections.GetDisplayNameOrDescription(member.Member);
            var value  = Lambdas.GetValue(expression);

            Add(name, desc, Conv.To <TValue>(value), newValue);
        }
コード例 #9
0
        public void TestGetValue_List()
        {
            var list = new List <string> {
                "a", "b"
            };
            Expression <Func <Sample, bool> > expression = t => list.Contains(t.StringValue);

            Assert.Equal(list, Lambdas.GetValue(expression));
        }
コード例 #10
0
        public void TestGetValue_Nullable()
        {
            Expression <Func <Sample, bool> > expression = t => t.NullableIntValue == 1;

            Assert.Equal(1, Lambdas.GetValue(expression));

            expression = t => t.NullableDecimalValue == 1.5M;
            Assert.Equal(1.5M, Lambdas.GetValue(expression));

            var sample = new Sample();

            expression = t => t.BoolValue == sample.NullableBoolValue;
            Assert.Null(Lambdas.GetValue(expression));
        }
コード例 #11
0
        /// <summary>
        /// 获取列
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <param name="right">是否取右侧操作数</param>
        private SqlItem GetColumn(Expression expression, bool right)
        {
            var type   = _resolver.GetType(expression, right);
            var column = _resolver.GetColumn(expression, type, right);

            if (string.IsNullOrWhiteSpace(column))
            {
                var name = _parameterManager.GenerateName();
                _parameterManager.Add(name, Lambdas.GetValue(expression));
                return(new SqlItem(name, raw: true));
            }

            return(new SqlItem(GetColumn(type, column)));
        }
コード例 #12
0
 /// <summary>
 /// 设置查询条件
 /// </summary>
 /// <typeparam name="TEntity">实体类型</typeparam>
 /// <param name="expression">查询条件表达式,如果参数值为空,则忽略该查询条件</param>
 public void WhereIfNotEmpty <TEntity>(Expression <Func <TEntity, bool> > expression) where TEntity : class
 {
     if (expression == null)
     {
         throw new ArgumentNullException(nameof(expression));
     }
     if (Lambdas.GetConditionCount(expression) > 1)
     {
         throw new InvalidOperationException(string.Format(LibraryResource.OnlyOnePredicate, expression));
     }
     if (string.IsNullOrWhiteSpace(Lambdas.GetValue(expression).SafeString()))
     {
         return;
     }
     Where(expression);
 }
コード例 #13
0
        public void TestGetValue_Static()
        {
            Expression <Func <Sample, bool> > expression = t => t.StringValue == Sample.StaticString;

            Assert.Equal("TestStaticString", Lambdas.GetValue(expression));

            expression = t => t.StringValue == Sample.StaticSample.StringValue;
            Assert.Equal("TestStaticSample", Lambdas.GetValue(expression));

            expression = t => t.Test2.StringValue == Sample.StaticString;
            Assert.Equal("TestStaticString", Lambdas.GetValue(expression));

            expression = t => t.Test2.StringValue == Sample.StaticSample.StringValue;
            Assert.Equal("TestStaticSample", Lambdas.GetValue(expression));

            expression = t => t.Test2.StringValue.Contains(Sample.StaticSample.StringValue);
            Assert.Equal("TestStaticSample", Lambdas.GetValue(expression));
        }
コード例 #14
0
        public void TestGetValue_Operation()
        {
            Expression <Func <Sample, bool> > expression = t => t.Test2.IntValue != 1;

            Assert.Equal(1, Lambdas.GetValue(expression));

            expression = t => t.Test2.IntValue > 1;
            Assert.Equal(1, Lambdas.GetValue(expression));

            expression = t => t.Test2.IntValue < 1;
            Assert.Equal(1, Lambdas.GetValue(expression));

            expression = t => t.Test2.IntValue >= 1;
            Assert.Equal(1, Lambdas.GetValue(expression));

            expression = t => t.Test2.IntValue <= 1;
            Assert.Equal(1, Lambdas.GetValue(expression));
        }
コード例 #15
0
        public void TestGetValue_Method()
        {
            Expression <Func <Sample, bool> > expression = t => t.StringValue.Contains("A");

            Assert.Equal("A", Lambdas.GetValue(expression));

            expression = t => t.Test2.StringValue.Contains("B");
            Assert.Equal("B", Lambdas.GetValue(expression));

            expression = t => t.Test2.Test3.StringValue.StartsWith("C");
            Assert.Equal("C", Lambdas.GetValue(expression));

            var test = new Sample {
                Email = "a"
            };

            expression = t => t.StringValue.Contains(test.Email);
            Assert.Equal("a", Lambdas.GetValue(expression));
        }
コード例 #16
0
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="expression">表达式</param>
        public object GetValue(Expression expression)
        {
            if (expression == null)
            {
                return(null);
            }
            var result = Lambdas.GetValue(expression);

            if (result == null)
            {
                return(null);
            }
            var type = result.GetType();

            if (type.IsEnum)
            {
                return(Enum.GetValue(type, result));
            }
            return(result);
        }
コード例 #17
0
        public void TestGetValue_Bool()
        {
            Expression <Func <Sample, bool> > expression = t => t.BoolValue;

            Assert.Equal("True", Lambdas.GetValue(expression).ToString());

            expression = t => !t.BoolValue;
            Assert.Equal("False", Lambdas.GetValue(expression).ToString());

            expression = t => t.Test2.BoolValue;
            Assert.Equal("True", Lambdas.GetValue(expression).ToString());

            expression = t => !t.Test2.BoolValue;
            Assert.Equal("False", Lambdas.GetValue(expression).ToString());

            expression = t => t.BoolValue == true;
            Assert.Equal("True", Lambdas.GetValue(expression).ToString());

            expression = t => t.BoolValue == false;
            Assert.Equal("False", Lambdas.GetValue(expression).ToString());
        }
コード例 #18
0
        public void TestGetValue_Enum()
        {
            var test1 = new Sample {
                NullableEnumValue = EnumSample.C
            };

            Expression <Func <Sample, bool> > expression = test => test.EnumValue == EnumSample.D;

            Assert.Equal(EnumSample.D.Value(), Enum.GetValue <EnumSample>(Lambdas.GetValue(expression)));

            expression = test => test.EnumValue == test1.NullableEnumValue;
            Assert.Equal(EnumSample.C, Lambdas.GetValue(expression));

            expression = test => test.NullableEnumValue == EnumSample.E;
            Assert.Equal(EnumSample.E, Lambdas.GetValue(expression));

            expression = test => test.NullableEnumValue == test1.NullableEnumValue;
            Assert.Equal(EnumSample.C, Lambdas.GetValue(expression));

            test1.NullableEnumValue = null;
            expression = test => test.NullableEnumValue == test1.NullableEnumValue;
            Assert.Null(Lambdas.GetValue(expression));
        }
コード例 #19
0
        public void TestGetValue_Complex()
        {
            var test = new Sample()
            {
                StringValue = "a", Test2 = new Sample2()
                {
                    StringValue = "b"
                }
            };

            Expression <Func <Sample, bool> > expression = t => t.StringValue == test.StringValue;

            Assert.Equal("a", Lambdas.GetValue(expression));
            Expression <Func <Sample, bool> > expression2 = t => t.StringValue == test.Test2.StringValue;

            Assert.Equal("b", Lambdas.GetValue(expression2));

            Expression <Func <Sample, bool> > expression3 = t => t.StringValue.Contains(test.StringValue);

            Assert.Equal("a", Lambdas.GetValue(expression3));
            Expression <Func <Sample, bool> > expression4 = t => t.StringValue.Contains(test.Test2.StringValue);

            Assert.Equal("b", Lambdas.GetValue(expression4));
        }
コード例 #20
0
        public void TestGetValue_Object()
        {
            Expression <Func <Sample, object> > expression = t => t.StringValue == "A";

            Assert.Equal("A", Lambdas.GetValue(expression));
        }