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