I have code that calculate average number of days from date opened to date closed.
When i run the expression using Linq and lambda expression i get error as follows:
Error message: could not resolve property: DateClosed.DateCreated of:
TestProject.LegalWork
where code is:
var result = Repository.All
.Where(x => x.DateClosed != null)
.Average(x => ((DateTime)x.DateClosed - x.DateCreated).TotalDays);
How ever when i run this using loop and filter on condition only, eveything work fine.
int number= 0;
decimal totalDays = 0;
foreach (LegalWork legalWork in Repository.All.Where(x => x.DateClosed != null))
{
totalDays += (decimal)((DateTime)legalWork.DateClosed - legalWork.DateCreated).TotalDays;
number++;
}
return (totalDays == 0 || numberOfLegalWork ==0 ) ? 0 : Convert.ToDecimal(totalDays/numberOfLegalWork);
What is wrong in Linq and Lambda version?
I’m sure you can’t call to:
or
givenDate.TotalDaysin linq2nhibernate, because you should call a specific function of DateTime which is not part of linq2nhibernate it’s part of .net framework and it doesn’t implemented in linq2nhibernate, but your current error message says another thing, to solve ur problem currently u can do:if there is problem with above code, please insert ur class definition,
but sure it’s not a best solution, it’s better to write a stored procedure and call it.