Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DbFunctions.DiffDays() results in: This function can only be invoked from LINQ to Entities

Where results1 is an IQuerable<myObject> that works fine until you try and filter by date:

results1 = results1.Where(l => DbFunctions.DiffDays(FromDate, l.LeadDate) >= 0);

And then I get this error:

This function can only be invoked from LINQ to Entities

I've seen a few other threads here that get me close to an answer, like this one, but it's just different enough that I'm not sure how to reformulate this filter so that it doesn't try and do this in memory (which is why I think the error is happening?)

EDIT:

The query was too complex to get it working in Linq. I thought this would be the same, but maybe not?

var query1 = @"
    SELECT 
        // columns that match the object 'myObject'
     FROM 
         // a whole bunch of joins and left joins
     ORDER BY
         ....";
 var results1 = Context.DbContext.Database.SqlQuery<myObject>(query1).AsQueryable();
like image 764
Casey Crookston Avatar asked Sep 20 '25 04:09

Casey Crookston


1 Answers

Can you use DateTime.Subtract and TimeSpan.Days?

results1 = results1.Where(l => FromDate.Subtract(l.LeadDate).Days >= 0).ToList();
like image 117
Rufus L Avatar answered Sep 22 '25 18:09

Rufus L