Lambda expressions - sum

2

I'm working with lambda expressions but I have a problem I have this method:

public static void Test_LambdaExpresions()
    {
        var albums = new List<Album>
        {
            new Album { Quantity = 10, Artist = "Betontod", Title = "Revolution" },
            new Album { Quantity = 50, Artist = "The Dangerous Summer", Title = "The Dangerous Summer" },
            new Album { Quantity = 200, Artist = "Depeche Mode", Title = "Spirit" },
            new Album { Quantity = 300, Artist = "Depeche Mode", Title = "other" },
            new Album { Quantity = 300, Artist = "Depeche Mode", Title = "Spirit" },
        };

        //left
        var parameter = Expression.Parameter(typeof(Album), "album");
        var comparison = Expression.GreaterThan(Expression.Property(parameter, Type.GetType("Test_LambdaExpresions.Album").GetProperty("Quantity")), Expression.Constant(100));
        Expression<Func<Album, bool>> expCo = null;
        expCo = Expression.Lambda<Func<Album, bool>>(comparison, parameter);



        //rigth
        var parameter2 = Expression.Parameter(typeof(Album), "album");
        var comparison2 = Expression.Equal(Expression.Property(parameter, Type.GetType("Test_LambdaExpresions.Album").GetProperty("Title")), Expression.Constant("Spirit"));
        Expression<Func<Album, bool>> expCo2 = null;
        expCo2 = Expression.Lambda<Func<Album, bool>>(comparison2, parameter2);

        Expression<Func<Album, bool>> finalExpression = Expresions<Album>.AndAlso(expCo, expCo2);

        Console.WriteLine(finalExpression.Body.ToString());
        var discountedAlbums = albums.Where(finalExpression.Compile());
    }

with this at the end I have a condition of this type:

  

((album.Quantity > 100) AndAlso (album.Title == "Spirit"))

but I need to add a constant value to the amount now, to get a where of the style:

  

((album.Quantity + 10 > 100) AndAlso (album.Title == "Spirit"))

How can I do this? This is a simple example. What I really need to accomplish is to add a few minutes to a date field and compare it with another date, but I wanted to start with something simpler. Is this possible? Thank you in advance

    
asked by Diana Cardenas 25.09.2018 в 01:32
source

1 answer

1

I found a solution to the specific issue of the dates I needed, I took it from this example: link

with this I get a where of the style:

  

album = > ((album.dateCreation.AddMinutes (30) > 09/25/2018 18:15:29) AndAlso (album.Title == "Spirit"))

        public static Expression AddOffset(string unit, double offset, Expression date)
    {
        switch (unit)
        {
            case "year":
                return Expression.Call(date, typeof(DateTime).GetMethod("AddYears"), Expression.Constant((int)offset));
            case "month":
                return Expression.Call(date, typeof(DateTime).GetMethod("AddMonths"), Expression.Constant((int)offset));
            case "week":
                return Expression.Call(date, typeof(DateTime).GetMethod("AddDays"), Expression.Constant(offset * 7));
            case "day":
                return Expression.Call(date, typeof(DateTime).GetMethod("AddDays"), Expression.Constant(offset));
            case "hour":
                return Expression.Call(date, typeof(DateTime).GetMethod("AddHours"), Expression.Constant(offset));
            case "minute":
                return Expression.Call(date, typeof(DateTime).GetMethod("AddMinutes"), Expression.Constant(offset));
            case "second":
                return Expression.Call(date, typeof(DateTime).GetMethod("AddSeconds"), Expression.Constant(offset));
        }

        return null;
    }

that method I use it in the following way:

            var parameter3 = Expression.Parameter(typeof(Album), "album");
        var param = Expression.Property(parameter, Type.GetType("Test_LambdaExpresions.Album").GetProperty("dateCreation"));
        Expression<Func<Album, bool>> expCo3 = null;


        Expression addMinutesDateExp = AddOffset("minute", 30, param);

        var comparison3 = Expression.GreaterThan(addMinutesDateExp, Expression.Constant(DateTime.Now));
        Console.WriteLine(addMinutesDateExp.ToString());
        expCo3 = Expression.Lambda<Func<Album, bool>>(comparison3, parameter3);

        Expression<Func<Album, bool>> finalExpression2 = Expresions<Album>.AndAlso(expCo3, expCo2);
    
answered by 26.09.2018 в 01:19