动态LINQ日期时间比较字符串构建-Linq To Entities [英] Dynamic LINQ DateTime Comparison String Building - Linq To Entities
问题描述
我正在使用Scott Guthrie的动态LINQ库以及Entity Framework和C#.
I'm using the dynamic LINQ library by Scott Guthrie together with Entity Framework and C#.
我必须基于几个因素将where字符串构建为变量,然后将字符串变量传递给where子句.由于某些原因,这将起作用:
I have to build my where string into a variable based on several factors and then pass the string variable to the where clause. For some reason, this will work:
ContactList = ContactList.Where("DateAdded >= @0", DateTime.Parse("12/1/2012"));
但这不起作用
string WhereClause = string.Format("DateAdded >= {0}", DateTime.Parse("12/1/2012"));
ContactList = ContactList.Where(WhereClause);
如上所述,我需要在传递变量的版本中使用它.有人知道第二个为什么不起作用吗?
As mentioned, I need to use it in the version of passing the variable. Anyone know why the second doesn't work?
提前谢谢!
推荐答案
似乎我要使用当前的DynamicLINQ库无法实现. Tilak在下面很好地概述了它不起作用的原因.
It seems what I was trying to do is not possible with the current DynamicLINQ library. The reason it didn't work was well outlined below by Tilak.
我的解决方案是修改DynamicLINQ库,以允许将查询写为字符串并传递给Date/Time数据类型的where子句. Paul Hatcher在此处找到了修改: LINQ TO SQL,具有DATE类型字段的动态查询
My solution was to modify the DynamicLINQ library to allow the query to be written as a string and passed to the where clause for Date/Time datatypes. The modification was found here by Paul Hatcher: LINQ TO SQL, Dynamic query with DATE type fields
这篇关于动态LINQ日期时间比较字符串构建-Linq To Entities的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!