如何在 IQueryable<> 中选择日期间隔?

发布于 2024-11-01 16:51:15 字数 1018 浏览 1 评论 0 原文

我有一个从实体模型收集的 IQueryable 。我想用它来获取一组新的 IQueryable 但仅限于网页上 2 个文本框的特定日期间隔内。

Journey 具有 Journey.DateFromJourney.DateTo,它们是字符串(“YYYYMMDD”)。

我以为我会做这样的事情:

(journeys is IQueryable)

if (tb_DateFrom.Text != ""){
    journeys = from j in journeys
               where Convert.ToInt32(j.DateTo) >= Convert.ToInt32(tb_DateFrom.Text)
               select j;
}
if (tb_DateTo.Text != ""){
        journeys = from j in journeys
                   where Convert.ToInt32(j.DateFrom) <= Convert.ToInt32(tb_DateTo.Text)
                   select j;
    }

但我收到错误消息说 linq 不知道如何做 Convert.ToInt32,它也不知道知道如何进行 int.parse 或 datetime.parse。有效的方法是使用 IEnumerable 而不是 IQueryable 但速度太慢以至于网站崩溃,因为我比较的数据非常大。

我该如何解决这个问题,这是将数据库中的格式转换为日期时间的唯一答案吗?

请帮忙:)

I have a IQueryable<Journey> that i collect from my entity model. I want to use this to get a new set of IQueryable<Journey> but only within a specific date interval from 2 textboxes on my webpage.

A Journey has Journey.DateFrom and Journey.DateTo which are strings ("YYYYMMDD").

I thought i would do something like this:

(journeys is IQueryable<Journey>)

if (tb_DateFrom.Text != ""){
    journeys = from j in journeys
               where Convert.ToInt32(j.DateTo) >= Convert.ToInt32(tb_DateFrom.Text)
               select j;
}
if (tb_DateTo.Text != ""){
        journeys = from j in journeys
                   where Convert.ToInt32(j.DateFrom) <= Convert.ToInt32(tb_DateTo.Text)
                   select j;
    }

But i get error saying that linq doesnt know how to do Convert.ToInt32, neither does it know how to do int.parse or datetime.parse. What works is to use IEnumerable<Journey> instead of IQueryable<Journey> but that is so slow that the website crash since the data im comparing is quite huge.

How can i work this out, is the only answer to get the format in db to datetime?

Please help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

故笙诉离歌 2024-11-08 16:51:15

我会尝试这个:

if (tb_DateFrom.Text != "") {
    journeys = from j in journeys
               where j.DateTo.CompareTo(tb_DateFrom.Text) >= 0
               select j;
}

if (tb_DateTo.Text != "") {
    journeys = from j in journeys
               where j.DateFrom.CompareTo(tb_DateTo.Text) <= 0
               select j;
}

I'd try this:

if (tb_DateFrom.Text != "") {
    journeys = from j in journeys
               where j.DateTo.CompareTo(tb_DateFrom.Text) >= 0
               select j;
}

if (tb_DateTo.Text != "") {
    journeys = from j in journeys
               where j.DateFrom.CompareTo(tb_DateTo.Text) <= 0
               select j;
}
谎言月老 2024-11-08 16:51:15

为什么不将文本框值转换为日期时间,然后比较 where 子句中的日期,而不是转换为 int

 DateTime? dateFrom = null, dateTo = null;

 if(!String.IsNullOrWhiteSpace(tb_DateFrom.Text))
    dateFrom = DateTime.ParseExact(tb_DateFrom.Text, "yyyyMMdd", null);

 if (!String.IsNullOrWhiteSpace(tb_DateTo.Text))
    dateTo = DateTime.ParseExact(tb_DateTo.Text, "yyyyMMdd", null);

 if (dateFrom.HasValue)
    journeys = journeys.Where(j => j.DateFrom >= dateFrom.Value);

 if (dateTo.HasValue)
    journeys = journeys.Where(j => j.DateTo <= dateTo.Value);

Why don't you convert textbox values to datetime and then compare the dates in the where clause, instead of converting to int

 DateTime? dateFrom = null, dateTo = null;

 if(!String.IsNullOrWhiteSpace(tb_DateFrom.Text))
    dateFrom = DateTime.ParseExact(tb_DateFrom.Text, "yyyyMMdd", null);

 if (!String.IsNullOrWhiteSpace(tb_DateTo.Text))
    dateTo = DateTime.ParseExact(tb_DateTo.Text, "yyyyMMdd", null);

 if (dateFrom.HasValue)
    journeys = journeys.Where(j => j.DateFrom >= dateFrom.Value);

 if (dateTo.HasValue)
    journeys = journeys.Where(j => j.DateTo <= dateTo.Value);
慈悲佛祖 2024-11-08 16:51:15
private DateTime getDate(string yyyyMmDd, DateTime defaultValue)
{
  DateTime ret  = DateTime.MinValue;
  if (!DateTime.TryParse(yyyyMmDd, out ret)) 
     return defaultValue;

  return ret;
}

var to = DateTime.Parse(tb_DateTo.Text);
var from  = DateTime.Parse(tb_DateFrom.Text);
journeys.Where(j=> getDate(j.DateFrom, DateTime.MaxValue) <= from && getDate(j.DateTo, DateTime.MinValue) >= to);
private DateTime getDate(string yyyyMmDd, DateTime defaultValue)
{
  DateTime ret  = DateTime.MinValue;
  if (!DateTime.TryParse(yyyyMmDd, out ret)) 
     return defaultValue;

  return ret;
}

var to = DateTime.Parse(tb_DateTo.Text);
var from  = DateTime.Parse(tb_DateFrom.Text);
journeys.Where(j=> getDate(j.DateFrom, DateTime.MaxValue) <= from && getDate(j.DateTo, DateTime.MinValue) >= to);
还不是爱你 2024-11-08 16:51:15

由于字符串格式的排序顺序与它们所代表的日期相同,我不明白为什么您必须转换它们的数据格式。只是做(未经测试):

journeys = from j in journeys
               where j.DateTo >= tb_DateFrom.Text && j.DateFrom >= tb_DateTo.Text
               select j;

更新,在Joakim的评论之后,仍然只使用字符串的排序顺序:

journeys = from j in journeys
           where j.DateTo.CompareTo(tb_DateFrom.Text) >= 0 && 
                 j.DateFrom.CompareTo(tb_DateTo.Text) <= 0
           select j;

(Det borde väl fungera,Joakim?)

哎呀,我错过了接受的答案,但我仍然会留下我的第一个编辑.. 。

As the string format you have sorts in the same order as the dates they represent, I don't see why you have to convert their data format at all. Just do (untested):

journeys = from j in journeys
               where j.DateTo >= tb_DateFrom.Text && j.DateFrom >= tb_DateTo.Text
               select j;

Update, after Joakim's comment, still just using the sort order of the strings:

journeys = from j in journeys
           where j.DateTo.CompareTo(tb_DateFrom.Text) >= 0 && 
                 j.DateFrom.CompareTo(tb_DateTo.Text) <= 0
           select j;

(Det borde väl fungera, Joakim?)

Oops, I missed the accepted answer, but I'll still leave my first edit...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文