DbFunctions.DiffDays()结果:此函数只能从LINQ到Entities调用 [英] DbFunctions.DiffDays() results in: This function can only be invoked from LINQ to Entities

查看:287
本文介绍了DbFunctions.DiffDays()结果:此函数只能从LINQ到Entities调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

results1IQuerable<myObject>的地方,在您尝试按日期进行过滤之前,它仍然可以正常工作

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);

然后我得到这个错误:

只能从LINQ到Entities调用此函数

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?)

查询过于复杂,无法在Linq中运行.我以为会是一样的,但也许不是吗?

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();

推荐答案

可以使用DateTime.SubtractTimeSpan.Days吗?

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

这篇关于DbFunctions.DiffDays()结果:此函数只能从LINQ到Entities调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆