开发者

How to select a date interval in 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 :)


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;
}


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);


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);


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜