LINQ to Entities不能识别方法'System.TimeSpan Subtract(System.DateTime)'方法 [英] LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method

查看:464
本文介绍了LINQ to Entities不能识别方法'System.TimeSpan Subtract(System.DateTime)'方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请在下面查看此查询。

$ b我尝试在60天内选择数据库中的记录,30天内20天内不同。
$ b

  var uploads =(
从_fileuploadRepository.Table中的文件
将_productRepository.Table中的产品加入到files.Event等于product.Id
其中

product.EventDate!= null&&b $ b(product.EventDate.Subtract(DateTime.Now).Days< = 60&&& .EventDate.Subtract(DateTime.Now).Days> = 60)||
(product.EventDate.Subtract(DateTime.Now).Days< = 30&& product.EventDate.Subtract(DateTime .Now).Days> = 30)||
(product.EventDate.Subtract(DateTime.Now).Days< = 20&& product.EventDate.Subtract(DateTime.Now).Days> ; = 20))
&&b $ b files.IsSkiped == false
选择文件;
).ToList();

但此查询发生错误。





我无能为力。请帮助。

解决方案

最简单的方法是解决执行查询之前的

  //仅评估DateTime.Now一次以达到一致性。你可能想要DateTime.Today代替。 
DateTime now = DateTime.Now;
DateTime nowPlus60Days = now.AddDays(60);
DateTime nowPlus30Days = now.AddDays(30);
DateTime nowPlus20Days = now.AddDays(20);

var query = ...
其中product.EventDate< = nowPlus60Days
...

请注意,您当前的查询甚至没有任何意义,因为每个或'd子句都表示给定的计算都小于或等于值大于或等于相同的值。如果你想要简单的等于然后使用它。如果没有,您不清楚您是否正在尝试。



如果您尝试将值分成少于20 ,20-30,30-60,60多个,您需要使用某种形式的分组。


I try to select records in database in 60 days 30 days 20 days differents in current date.

Please see this query in below.

 var uploads = (
                from files in _fileuploadRepository.Table
                join product in _productRepository.Table on files.Event equals product.Id
                where
                    (
                product.EventDate != null &&
                    (product.EventDate.Subtract(DateTime.Now).Days <= 60 && product.EventDate.Subtract(DateTime.Now).Days >= 60) ||
                    (product.EventDate.Subtract(DateTime.Now).Days <= 30 && product.EventDate.Subtract(DateTime.Now).Days >= 30) ||
                    (product.EventDate.Subtract(DateTime.Now).Days <= 20 && product.EventDate.Subtract(DateTime.Now).Days >= 20))
                    &&
                files.IsSkiped == false
                select files;
            ).ToList();

But a error occurred this query.

I am clueless. Please Help.

解决方案

The simplest approach is to work out the bounds before you perform the query:

// Only evaluate DateTime.Now once for consistency. You might want DateTime.Today instead.
DateTime now = DateTime.Now;
DateTime nowPlus60Days = now.AddDays(60);
DateTime nowPlus30Days = now.AddDays(30);
DateTime nowPlus20Days = now.AddDays(20);

var query = ...
            where product.EventDate <= nowPlus60Days
            ...

Note that your current query doesn't even really make sense, as each "or"'d clause is stating that the given computation is both less than or equal to a value and greater than or equal to the same value. If you want simple "equal to" then use that. If not, it's not clear what you are trying to do.

If you're trying to bucket the values into "less than 20", "20-30", "30-60", "more than 60" you'll need to use grouping of some form.

这篇关于LINQ to Entities不能识别方法'System.TimeSpan Subtract(System.DateTime)'方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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