在动态LINQ到实体中使用DateTime [英] Using DateTime in Dynamic LINQ to Entities

查看:100
本文介绍了在动态LINQ到实体中使用DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LINQ to Entities来检索项目购买日期,如下所示:

 其中EntityFunctions.TruncateTime(order.PurchaseDate) == myPurchaseDate.date 

这里的关键是DB列包含日期和时间,所以时间必须剥离才能进行比较。此代码工作正常。



现在,我想使用动态LINQ to Entity来做同样的事情。我正在使用VS2010代码示例文件夹中的dynamic.cs。当我编码:

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

或任何相同的变体我收到一条错误消息。我需要用代码作为字符串值来使其工作吗? (因为我可以使用.StartsWith或.Contains里面的字符串我希望有一些日期功能动态LINQ会识别)。



我知道我可以创建动态LINQ查询成为日期范围,概念上:

  PurchaseDate> = myPurchaseDate @午夜和PurchaseDate< = myPurchaseDate + 23:59实际上,也许一个日期范围从SQL Server的角度看是更有效率的,但是我希望要知道动态LINQ to Entities中是否存在TruncateTime或ToShortDate等。

解决方案

我最近开始使用动态linq作为项目,也想比较没有时间分量的日期。 Microsoft的动态linq C#示例代码(Dynamic.cs)支持一组固定类型,而 EntityFunctions 不是其中之一。



但是通过一些实验,我发现只要将 EntityFunctions 添加到预定义类型的数组中,就可以使用 TruncateTime 和可能的其他实体功能方法。



以下是我的项目中的Dynamic.cs预定义类型数组:

  static readonly Type []definedTypes = {
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
};

使用此修改的Dynamic.cs文件,我可以创建动态linq查询,包括像PurchaseDate示例在您的问题。


I am using LINQ to Entities to retrieve item purchase dates as follows:

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. This code works fine.

Now, I want to do the same thing using dynamic LINQ to Entities. I am using dynamic.cs from the VS2010 code samples folder. 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).

I know I can create the dynamic LINQ query to be a date range, conceptually:

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.

解决方案

I recently started using dynamic linq for a project and also wanted to compare dates without the time component. Microsoft's dynamic linq C# sample code (Dynamic.cs) supports a fixed set of types, and EntityFunctions isn't one of them.

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.

Here's what the Dynamic.cs predefinedTypes array looks like in my project:

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.

这篇关于在动态LINQ到实体中使用DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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