LINQ到实体无法识别方法“的Int64马克斯(的Int64,Int64的)”的方法,而这种方法不能被翻译成表达店 [英] LINQ to Entities does not recognize the method 'Int64 Max(Int64, Int64)' method, and this method cannot be translated into a store expression

查看:327
本文介绍了LINQ到实体无法识别方法“的Int64马克斯(的Int64,Int64的)”的方法,而这种方法不能被翻译成表达店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到运行时此错误消息

I'm getting this error message during runtime

LINQ到实体无法识别方法的Int64马克斯(的Int64,Int64的) '法,这种方法不能被翻译成店表达

LINQ to Entities does not recognize the method 'Int64 Max(Int64, Int64)' method, and this method cannot be translated into a store expression.

当我尝试这样做:

return _dbContext.Appointment.Where(x => Math.Max(x.Appointment.StartTime.Ticks, startTime.Ticks) <= Math.Min(x.Appointment.EndTime.Ticks, endTime.Ticks));



此查询背后的想法是,如果最迟开始时间是最早的结束时间之前,则你有日期和时间部分重叠/感人。

The idea behind this query is that "if the latest start time is before the earliest end time, then you have some overlap/touching" in date and time.

有没有什么办法让这条线的工作?我已经选中,如果 EntityFunctions 有'东西',这是情况并非如此。

Is there any way to get this line working? I already checked if EntityFunctions has 'something', which wasn't the case.

推荐答案

您必须始终牢记,每一个IQueryable的提供者以自己的方式来实现。该查询中的LINQ工作对象(因为它不被转换到任何东西),但可能会或可能不会IQueryable的提供商合作。

You must always keep in mind that every IQueryable provider is implemented in its own way. That query would work in Linq to Objects (as it does not gets translated to anything) but may or may not work in IQueryable providers.

在特定的情况下,它告诉你认为它如何Math.Max和Math.Min转化为SQL命令不知道。这是完全正确的,因为EF不承认这些方法。

In your specific case, it's telling you that it has no idea on how to translate Math.Max and Math.Min into SQL commands. And that's perfectly correct, since EF doesn't recognize those methods.

在我看来来完成你需要的是读取最大和最小值的两个最简单的方法不同的查询,然后做你的逻辑在纯C#。事情是这样的:

In my opinion the easiest way to accomplish what you need is reading the max and the min value with two different queries and then do your logic in plain c#. Something like this:

var min = mycontext.MyDbSet.Min(c => c.Field);
var max = mycontext.MyDbSet.Max(c => c.Field);
if (max <= min)
{
    // do something
}

这篇关于LINQ到实体无法识别方法“的Int64马克斯(的Int64,Int64的)”的方法,而这种方法不能被翻译成表达店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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