LINQ to Entities不支持“Ticks” [英] 'Ticks' is not supported in LINQ to Entities

查看:245
本文介绍了LINQ to Entities不支持“Ticks”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个我的代码:

 var count = this.Repository.ObjectContext.LogDetail
                .Count(p => p.LogId == logId &&System.Data.Objects.EntityFunctions. p.LogDetailTime. == logDetailTime.Ticks && p.OperationId == operationId);
            return (count > 0);

我收到此错误:


LINQ to
实体不支持指定的类型成员Ticks。只支持初始化设备,实体成员和实体导航
属性。

The specified type member 'Ticks' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

我如何解决?

How can i resolve it?

推荐答案

如上所述,您不应该将所有记录都记录到内存中,然后进行过滤。您可以轻松地比较日期值而不转换为刻度:

As said, you should not fetch all records into memory and then do the filtering. You can easily compare the date values without converting to ticks:

var query = this.Repository.ObjectContext.LogDetail
            .Where(p => p.LogId == logId 
                     && p.OperationId == operationId
                     && EntityFunctions.DiffSeconds(p.LogDetailTime,
                                                      logDetailTime) == 0);
return (query.Any());

请注意, DiffSeconds 可能会导致溢出该差异大于 Int32.MaxValue (错误: datediff函数导致溢出)。我不知道你的精准程度,以及比较值相差多远,所以你必须在这里做出选择。你真的对具有完全相同数量的蜱的记录感兴趣吗?很难想象可能 DiffMinutes 是够好的。

Note that DiffSeconds can cause an overflow when the difference becomes larger than Int32.MaxValue (Error: The datediff function resulted in an overflow). I don't know what precision you're after and how far the compared values are apart, so you have to make a choice here. Were you really interested in records with exactly the same amount of ticks? Hard to imagine. Maybe DiffMinutes is good enough.

另一个可能的优化是使用任何而不是 Count 任何生成 WHERE EXISTS 子句。这可能比SQL中 COUNT 更有效。

Another possible optimization is to use Any in stead of Count. Any generates an WHERE EXISTS clause. This may be more efficient than COUNT in SQL.

你获得的是现在只有一个布尔(位)被转移代替潜在的大量记录。

What you gain is that now only a boolean (bit) is transferred in stead of a potentially large number of records.

注意:在实体框架6 EntityFunctions 已被替换为 DbFunctions

Note: In Entity Framework 6 EntityFunctions has been replaced by DbFunctions.

这篇关于LINQ to Entities不支持“Ticks”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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