I have a lambda expression in which need to assign a local variable to avoid calling my RetrieveAge(Datetime birthDate) two times per resultset.
My lambda expression looks as following:
result = myList.AsEnumerable().Where(f => DateHelper.RetrieveAge(f.Birthdate) >= 20 && DateHelper.RetrieveAge(f.Birthdate) <= 40).Select(x => new Person { Name = x.Name, Id = x.Id, Alias = x.Alias }).ToList();
I am trying to achieve something like the following:
var result = myList.AsEnumerable().Where(f => { var age = DateHelper.RetrieveAge(f.Birthdate); age >= 20 && age <= 40 }).Select(x => new Person { Name = x.Name, Id = x.Id, Alias = x.Alias }).ToList();
But I can't figure out how to do that properly. Any hints or suggestions would be mostly appreciated.