Example #1
0
        public void DynamicExpressionParser_ParseLambda_Operator_Less_Greater_With_Guids()
        {
            var config = new ParsingConfig
            {
                CustomTypeProvider = new TestCustomTypeProvider()
            };

            // Arrange
            Guid someId    = Guid.NewGuid();
            Guid anotherId = Guid.NewGuid();
            var  user      = new User();

            user.Id = someId;
            Guid   guidEmpty      = Guid.Empty;
            string expressionText =
                $"iif(@0.Id == StaticHelper.GetGuid(\"name\"), Guid.Parse(\"{guidEmpty}\"), Guid.Parse(\"{anotherId}\"))";

            // Act
            var lambda     = DynamicExpressionParser.ParseLambda(config, typeof(User), null, expressionText, user);
            var guidLambda = lambda as Expression <Func <User, Guid> >;

            Assert.NotNull(guidLambda);

            var  del    = lambda.Compile();
            Guid result = (Guid)del.DynamicInvoke(user);

            // Assert
            Assert.Equal(anotherId, result);
        }
Example #2
0
        public void DynamicExpressionParser_ParseLambda_Complex_3()
        {
            var config = new ParsingConfig
            {
                CustomTypeProvider = new TestCustomTypeProvider()
            };

            // Arrange
            var testList = User.GenerateSampleModels(51);
            var qry      = testList.AsQueryable();

            var externals = new Dictionary <string, object>
            {
                { "Users", qry }
            };

            // Act
            string stringExpression =
                "Users.GroupBy(x => new { x.Profile.Age }).OrderBy(gg => gg.Key.Age).Select(j => new System.Linq.Dynamic.Core.Tests.DynamicExpressionParserTests+ComplexParseLambda3Result{j.Key.Age, j.Sum(k => k.Income) As TotalIncome})";
            LambdaExpression expression =
                DynamicExpressionParser.ParseLambda(config, null, stringExpression, externals);
            Delegate del = expression.Compile();
            IEnumerable <dynamic> result = del.DynamicInvoke() as IEnumerable <dynamic>;

            var expected = qry.GroupBy(x => new { x.Profile.Age }).OrderBy(gg => gg.Key.Age)
                           .Select(j => new ComplexParseLambda3Result {
                Age = j.Key.Age, TotalIncome = j.Sum(k => k.Income)
            })
                           .Cast <dynamic>().ToArray();

            // Assert
            Check.That(result).IsNotNull();
            Check.That(result).HasSize(expected.Length);
            Check.That(result.ToArray()[0]).Equals(expected[0]);
        }
Example #3
0
        public void DynamicExpressionParser_ParseLambda_Complex_2()
        {
            // Arrange
            var testList = User.GenerateSampleModels(51);
            var qry      = testList.AsQueryable();

            // Act
            string query =
                "GroupBy(x => new { x.Profile.Age }, it).OrderBy(gg => gg.Key.Age).Select(j => new (j.Key.Age, j.Sum(k => k.Income) As TotalIncome))";
            LambdaExpression      expression = DynamicExpressionParser.ParseLambda(qry.GetType(), null, query);
            Delegate              del        = expression.Compile();
            IEnumerable <dynamic> result     = del.DynamicInvoke(qry) as IEnumerable <dynamic>;

            var expected = qry.GroupBy(x => new { x.Profile.Age }, x => x).OrderBy(gg => gg.Key.Age)
                           .Select(j => new { j.Key.Age, TotalIncome = j.Sum(k => k.Income) })
                           .Select(c => new ComplexParseLambda1Result {
                Age = c.Age, TotalIncome = c.TotalIncome
            }).Cast <dynamic>()
                           .ToArray();

            // Assert
            Check.That(result).IsNotNull();
            Check.That(result).HasSize(expected.Length);
            Check.That(result.ToArray()[0]).Equals(expected[0]);
        }
Example #4
0
        public void DynamicExpressionParser_ParseLambda_ToList()
        {
            // Arrange
            var testList = User.GenerateSampleModels(51);
            var qry      = testList.AsQueryable();

            // Act
            string                query      = "OrderBy(gg => gg.Income).ToList()";
            LambdaExpression      expression = DynamicExpressionParser.ParseLambda(qry.GetType(), null, query);
            Delegate              del        = expression.Compile();
            IEnumerable <dynamic> result     = del.DynamicInvoke(qry) as IEnumerable <dynamic>;

            var expected = qry.OrderBy(gg => gg.Income).ToList();

            // Assert
            Check.That(result).IsNotNull();
            Check.That(result).HasSize(expected.Count);
            Check.That(result.ToArray()[0]).Equals(expected[0]);
        }
Example #5
0
        public void DynamicExpressionParser_ParseLambda_With_Null_Equals_Guid()
        {
            // Arrange
            var    user           = new User();
            Guid   guidEmpty      = Guid.Empty;
            Guid   someId         = Guid.NewGuid();
            string expressionText = $"iif(null == @0.Id, @0.Id == Guid.Parse(\"{someId}\"), Id == Id)";

            // Act
            var lambda     = DynamicExpressionParser.ParseLambda(typeof(User), null, expressionText, user);
            var boolLambda = lambda as Expression <Func <User, bool> >;

            Assert.NotNull(boolLambda);

            var  del    = lambda.Compile();
            bool result = (bool)del.DynamicInvoke(user);

            // Assert
            Assert.True(result);
        }
Example #6
0
        public void DynamicExpressionParser_ParseLambda_Select_2()
        {
            // Arrange
            var testList = User.GenerateSampleModels(5);
            var qry      = testList.AsQueryable();

            var externals = new Dictionary <string, object>
            {
                { "Users", qry }
            };

            // Act
            string           query      = "Users.Select(j => j)";
            LambdaExpression expression = DynamicExpressionParser.ParseLambda(null, query, externals);
            Delegate         del        = expression.Compile();
            object           result     = del.DynamicInvoke();

            // Assert
            Assert.NotNull(result);
        }
Example #7
0
        public void DynamicExpressionParser_ParseLambda_WithStructWithEquality()
        {
            // Assign
            var testList = User.GenerateSampleModels(51);
            var qry      = testList.AsQueryable();

            // Act
            ulong expectedX = (ulong)long.MaxValue + 3;

            string                query      = $"Where(x => x.SnowflakeId == {expectedX})";
            LambdaExpression      expression = DynamicExpressionParser.ParseLambda(qry.GetType(), null, query);
            Delegate              del        = expression.Compile();
            IEnumerable <dynamic> result     = del.DynamicInvoke(qry) as IEnumerable <dynamic>;

            var expected = qry.Where(gg => gg.SnowflakeId == new SnowflakeId(expectedX)).ToList();

            // Assert
            Check.That(result).IsNotNull();
            Check.That(result).HasSize(expected.Count);
            Check.That(result.ToArray()[0]).Equals(expected[0]);
        }
Example #8
0
        public void DynamicExpressionParser_ParseLambda_With_Guid_Equals_String()
        {
            // Arrange
            Guid someId    = Guid.NewGuid();
            Guid anotherId = Guid.NewGuid();
            var  user      = new User();

            user.Id = someId;
            Guid   guidEmpty      = Guid.Empty;
            string expressionText =
                $"iif(@0.Id == \"{someId}\", Guid.Parse(\"{guidEmpty}\"), Guid.Parse(\"{anotherId}\"))";

            // Act
            var lambda     = DynamicExpressionParser.ParseLambda(typeof(User), null, expressionText, user);
            var guidLambda = lambda as Expression <Func <User, Guid> >;

            Assert.NotNull(guidLambda);

            var  del    = lambda.Compile();
            Guid result = (Guid)del.DynamicInvoke(user);

            // Assert
            Assert.Equal(guidEmpty, result);
        }