简体   繁体   中英

How to select range of dates from list on providing searching criteria

   DateInfo[] dateInfo= GetDateInfo();

   DateInfo[] searchDates  = null;
   DateTime dtfrom = DateTime.Parse(txtFromDate.Text);
   DateTime dtTo = DateTime.Parse(txtToDate.Text);
   searchDates = dateInfo.Where(p => p.AddDate >= dtfrom || p.AddDate < dtTo).ToArray<DateInfo>();

if list contain

11/17/13,11/18/13,11/19/13,11/19/13,11/19/13,11/20/13,11/20/13 

and I have entered

11/17/13 to 11/19/13 

it should return

11/17/13,11/18/13,11/19/13,11/19/13,11/19/13

Replace || with && :

searchDates = dateInfo.Where(p => p.AddDate >= dtfrom && p.AddDate <= dtTo)
                      .ToArray();
searchDates = dateInfo
   .Where(p => p.AddDate >= dtfrom && p.AddDate <= dtTo)
   .ToArray<DateInfo>();

AND in this case will force to filter dates that are between, while your condition will cover everything.

感谢您的回答。我的问题通过编写此查询得以解决

searchDates  = dateInfo.Where(p => (p.AddDate.Date.Year >= dtfrom.Date.Year && p.AddDate.Date.Month >= dtfrom.Date.Month && p.AddDate.Date.Day >= dtfrom.Date.Day) && (p.AddDate.Date.Year <= dtTo.Date.Year && p.AddDate.Date.Month <= dtTo.Date.Month && p.AddDate.Date.Day <= dtTo.Date.Day)).ToArray<DateInfo>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM