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

Where results1

is this IQuerable<myObject>

, which works great until you try to filter by date:

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

      

And then I get this error:

This function can only be called from LINQ to Entities

I've seen several other threads here that bring me closer to an answer like this one , but it's just different from the fact that I'm not sure how to reformulate this filter so that it doesn't try and do it in memory (so I think the error is happening?)

EDIT:

The query was too complex to make it work in Linq. I thought it would be the same thing, 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();

      

+3


source to share


2 answers


Can you use DateTime.Subtract

and TimeSpan.Days

?



results1 = results1.Where(l => FromDate.Subtract(l.LeadDate).Days >= 0).ToList();

      

+1


source


Since you are using a raw SQL query (i.e. using SqlQuery

), the result is a materialized dataset. This means that the request has already been submitted to the data store and the data has been submitted as DbRawSqlQuery<>

. This is not the same kind of interface you can use as you usually extend the EF request.



You can either add to the raw SQL query, use a method from the .NET Framework, or write your own.

+1


source







All Articles