Linq to EF 表达式树/谓词 int.Parse 解决方法
我有一个名为 Enquiry 的 linq 实体,它有一个属性:字符串 DateSubscribed。
我正在编写一个应用程序,我需要返回 IQueryable 进行查询,该查询在特定日期范围内具有 DateSubscribed。
理想情况下,我想写一些像
IQueryable<Enquiry> query = Context.EnquirySet.AsQueryable<Enquiry>();
int dateStart = int.Parse("20090729");
int dateEnd = int.Parse("20090930");
query = (from e in query
where(enq => int.Parse(enq.DateSubmitted) < dateEnd)
where(enq => int.Parse(enq.DateSubmitted) > dateStart)
select e);
“显然Linq to EF不识别int.Parse”这样的东西,所以我认为我可以使用返回谓词的Expression方法来实现我想要的东西???
我一直在玩 PredicateBuilder 并四处寻找,但我已经成功地绞尽脑汁试图解决这个问题。当然,我可以向我的实体添加另一个属性并将其转换在那里,但我真的很想了解这一点。任何人都可以解释或给出一个不会烧坏我的大脑的示例/链接吗?
预先感谢
马克
I have a linq Entity called Enquiry, which has a property: string DateSubmitted.
I'm writing an app where I need to return IQueryable for Enquiry that have a DateSubmitted within a particular date range.
Ideally I'd like to write something like
IQueryable<Enquiry> query = Context.EnquirySet.AsQueryable<Enquiry>();
int dateStart = int.Parse("20090729");
int dateEnd = int.Parse("20090930");
query = (from e in query
where(enq => int.Parse(enq.DateSubmitted) < dateEnd)
where(enq => int.Parse(enq.DateSubmitted) > dateStart)
select e);
Obviously Linq to EF doesn't recognise int.Parse, so I think I can achieve what I want with an Expression method that returns a predicate???
I've been playing around with PredicateBuilder and looking all over but I've successfully fried my brains trying to work this out. Sure I could add another property to my Entity and convert it there but I'd really like to understand this. Can anyone explain or give an example/link that doesn't fry my brains?
Thanks in advance
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您知道日期字符串是有效的,并且它们确实按照该顺序(这是自然排序顺序),您可能可以摆脱字符串比较:
If you know your date strings are valid, and they're really in that order (which is a natural sort order) you might be able to get away with string comparisons: