Linq static method error
I have created following function to get dates difference:

        public static double MonthsDifference(DateTime dtStart, DateTime dtNow)
        {
            DateTime start = dtStart;
            DateTime end = dtNow;
            int compMonth = (end.Month + end.Year * 12) - (start.Month + start.Year * 12);
            double daysInEndMonth = (end - end.AddMonths(1)).Days;
            double months = compMonth + (start.Day - end.Day) / daysInEndMonth;
            return months;
        }

I am using it in my LINQ query

 

    var query = from c in context.AccountOwners.Where( MonthsDifference(p.Account.StateChangeDate,DateTime.Now) < 4 )
                            select c;
                return query.Count();

but it is giving error:

LINQ to Entities does not recognize the method 'Double MonthsDifference(System.DateTime, System.DateTime)' method, and this method cannot be translated into a store expression. 

Please suggest solution


Linq static method error
===

I have created following function to get dates difference:

        public static double MonthsDifference(DateTime dtStart, DateTime dtNow)
        {
            DateTime start = dtStart;
            DateTime end = dtNow;
            int compMonth = (end.Month + end.Year * 12) - (start.Month + start.Year * 12);
            double daysInEndMonth = (end - end.AddMonths(1)).Days;
            double months = compMonth + (start.Day - end.Day) / daysInEndMonth;
            return months;
        }

I am using it in my LINQ query

 

    var query = from c in context.AccountOwners.Where( MonthsDifference(p.Account.StateChangeDate,DateTime.Now) < 4 )
                            select c;
                return query.Count();

but it is giving error:

LINQ to Entities does not recognize the method 'Double MonthsDifference(System.DateTime, System.DateTime)' method, and this method cannot be translated into a store expression. 

Please suggest solution

