简体   繁体   English

在Dynamic LINQ to Entities中使用DateTime

[英]Using DateTime in Dynamic LINQ to Entities

I am using LINQ to Entities to retrieve item purchase dates as follows: 我使用LINQ to Entities来检索项目购买日期,如下所示:

where EntityFunctions.TruncateTime(order.PurchaseDate) == myPurchaseDate.date

The key here is that the DB column contains the Date and Time so the time must be stripped for the compare. 这里的关键是DB列包含日期和时间,因此必须剥离比较的时间。 This code works fine. 这段代码工作正常。

Now, I want to do the same thing using dynamic LINQ to Entities. 现在,我想使用动态LINQ to Entities做同样的事情。 I am using dynamic.cs from the VS2010 code samples folder. 我正在使用VS2010代码示例文件夹中的dynamic.cs。 When I code: 当我编码:

.where("EntityFunctions.TruncateTime(PurchaseDate) == @0", myPurchaseDate.date);

or any variant of same I get an error message. 或者我的任何变体都会收到错误消息。 What do I have to code as the string value to make this work? 我需要编写什么作为字符串值才能使其工作? (Since I can use .StartsWith or .Contains inside the string I am hoping there is some date function dynamic LINQ will recognize). (因为我可以在字符串中使用.StartsWith或.Contains,我希望有一些日期函数动态LINQ会识别)。

I know I can create the dynamic LINQ query to be a date range, conceptually: 我知道我可以创建动态LINQ查询作为日期范围,从概念上讲:

PurchaseDate >= myPurchaseDate@midnight and PurchaseDate <= myPurchaseDate+23:59:59

In fact, maybe a date range is more efficient from a SQL Server perspective but I would like to know if something like TruncateTime or ToShortDate exists within Dynamic LINQ to Entities. 实际上,从SQL Server的角度来看,日期范围可能更有效,但我想知道在动态LINQ到实体中是否存在类似TruncateTime或ToShortDate的内容。

I recently started using dynamic linq for a project and also wanted to compare dates without the time component. 我最近开始在项目中使用动态linq,并且还希望在没有时间组件的情况下比较日期。 Microsoft's dynamic linq C# sample code (Dynamic.cs) supports a fixed set of types, and EntityFunctions isn't one of them. Microsoft的动态linq C#示例代码(Dynamic.cs)支持一组固定的类型,而EntityFunctions不是其中之一。

But with a little experimentation, I found that just adding EntityFunctions to the array of predefined types enables the use of TruncateTime and likely other EntityFunctions methods too. 但通过一些实验,我发现只需将EntityFunctions添加到预定义类型的数组中,就可以使用TruncateTime和其他EntityFunctions方法。

Here's what the Dynamic.cs predefinedTypes array looks like in my project: 这是Dynamic.cs predefinedTypess数组在我的项目中的样子:

static readonly Type[] predefinedTypes = {
    typeof(Object),
    typeof(Boolean),
    typeof(Char),
    typeof(String),
    typeof(SByte),
    typeof(Byte),
    typeof(Int16),
    typeof(UInt16),
    typeof(Int32),
    typeof(UInt32),
    typeof(Int64),
    typeof(UInt64),
    typeof(Single),
    typeof(Double),
    typeof(Decimal),
    typeof(DateTime),
    typeof(TimeSpan),
    typeof(Guid),
    typeof(Math),
    typeof(Convert),
    typeof(System.Data.Objects.EntityFunctions)             // JimM
};

With this modified Dynamic.cs file, I'm able to create dynamic linq queries including expressions like the PurchaseDate example in your question. 使用这个经过修改的Dynamic.cs文件,我可以创建动态linq查询,包括问题中的PurchaseDate示例等表达式。

If your queries will often search by just the date aspect, another approach to consider, if available with your SQL Server database, would be to redundantly store a truncated version of the datetime column, in a date column type. 如果您的查询通常仅按日期方面进行搜索,则另一种方法(如果SQL Server数据库可用)将在日期列类型中冗余地存储日期时间列的截断版本。 See http://msdn.microsoft.com/en-us/library/bb630352.aspx ). 请参阅http://msdn.microsoft.com/en-us/library/bb630352.aspx )。

All of your queries can then perform better because there are no conversions necessary, are less prone to developer error. 然后,您的所有查询都可以更好地执行,因为没有必要的转换,不太容易出现开发人员错误。 And they're easier to query in plain old SQL as well. 并且它们在普通的旧SQL中也更容易查询。

Your EF queries would then query on the SQL Server date column 然后,您的EF查询将在SQL Server日期列上进行查询

I think you are going to want to go with the date range so that you can take advantage of indexing on the server if you need to. 我想你会想要使用日期范围,以便在需要时可以利用服务器上的索引。 If you base your select statement on a calculated field, it will need to scan the table for each query. 如果将select语句基于计算字段,则需要扫描每个查询的表。

Here is a link that lists EF4 functions that map to sql functions 这是一个列出映射到sql函数的EF4函数的链接

Conceptual Model Canonical to SQL Server Functions Mapping SQL Server函数映射的概念模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM