Search code examples
c#entity-frameworklinqnotsupportedexceptiondbfunctions

DbFunction.DiffMinutes { 'System.NotSupportedException' }


I want write this code this part of my code

    var today = DateTime.Now;
    var todayString = today.ToShortDateString();
    List<FB> fBList = _context.FBs.ToList();
    fBList = fBList.Where(x => DbFunctions.DiffMinutes(x.FB_CDate, x.FB_LTDate) > 20 ).ToList();

and this part return me this

    'DbFunctions.DiffMinutes(x.FB_CDate, x.FB_LTDate) > 20' threw an exception of type 'System.NotSupportedException'

Solution

  • DbFunctions provides CLR methods that get translated to database functions when used in LINQ to Entities queries. When you call ToList(),data is loaded into memory and DbFunctions causes an error. You need to change your code as follows:

     var today = DateTime.Now;
                var todayString = today.ToShortDateString();
                List<FB> fBList = _context.FBs.Where(x => DbFunctions.DiffMinutes(x.FB_CDate, x.FB_LTDate) > 20).ToList();