Linq to EF 表达式树/谓词 int.Parse 解决方法

发布于 2024-08-06 10:04:55 字数 738 浏览 7 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若能看破又如何 2024-08-13 10:04:55

如果您知道日期字符串是有效的,并且它们确实按照该顺序(这是自然排序顺序),您可能可以摆脱字符串比较:

IQueryable<Enquiry> query = Context.EnquirySet.AsQueryable<Enquiry>();
string dateStart ="20090729";
string dateEnd = "20090930";

query = (from e in query
     where(enq => enq.DateSubmitted.CompareTo(dateEnd)) < 0)
     where(enq => enq.DateSubmitted.CompareTo(dateStart)) > 0)
     select e);

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:

IQueryable<Enquiry> query = Context.EnquirySet.AsQueryable<Enquiry>();
string dateStart ="20090729";
string dateEnd = "20090930";

query = (from e in query
     where(enq => enq.DateSubmitted.CompareTo(dateEnd)) < 0)
     where(enq => enq.DateSubmitted.CompareTo(dateStart)) > 0)
     select e);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文