CS0834 - A lambda expression with a statement body cannot be converted to an expression tree

55 Views Asked by At

I am trying to create an Expression to filter the profiles by creation date

here is the code

private static Expression<Func<Profile, bool>> MonthsPeriodFilter = profile =>
{
    var Date1 = DateTime.Now;
    var Date2 = profile.creationDate;

    if (!Date2.HasValue)
    {
        return false; 
    }

    var eDate = Date1 < Date2.Value ? Date1 : Date2.Value;
    var lDate = Date1 < Date2.Value ? Date2.Value : Date1 ;

    var difference = (lDate.Year - eDate.Year) * 12 + lDate.Month - eDate.Month + (eDate.Day <= lDate.Day ? 0 : -1);

    return difference <= 12;
};

but i have this error:

A lambda expression with a statement body cannot be converted to an expression tree

0

There are 0 best solutions below