Reputation: 1477
I got a list of dates that serves as attendance like
06/01/2012
06/02/2012
06/03/2012
06/06/2012
06/07/2012
06/08/2012
06/10/2012
06/13/2012
06/14/2012
06/15/2012
and i have this date range
06/01/2012-06/15/2012
How would i get the dates that are not on the list but falls in the date range using linq..
Upvotes: 0
Views: 162
Reputation: 29026
Let inputDateList
be a List of strings representing the input dates, and AttendanceDate
a List of DateTime corresponds to the input strings. Let me use dateRangeStart
to denote the start range and dateRangeEnd
for the range End. Then following code will filter the inputDateList within the range of dateRangeStart - dateRangeEnd.
DateTime inDate;
DateTime dateRangeStart;
DateTime dateRangeEnd;
List<string> inputDateList = new List<string>(){"06/01/2012",
"06/02/2012",
"06/03/2012",
"06/06/2012",
"06/07/2012",
"06/08/2012",
"06/10/2012",
"06/13/2012",
"06/14/2012",
"06/17/2012"};
List<DateTime?> AttendanceDate = inputDateList.Select(x => DateTime.TryParseExact(x, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out inDate) ? inDate : (DateTime?)null)
.ToList();
DateTime.TryParseExact("06/01/2012", "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateRangeStart);
DateTime.TryParseExact("06/15/2012", "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateRangeEnd);
List<DateTime?> datesInRange = AttendanceDate.Where(x => x >= dateRangeStart && x <= dateRangeEnd).ToList();
datesInRange
will be the required List of dates
Upvotes: 2
Reputation: 27357
You can do something like this:
var dateList = new List<DateTime>
{
new DateTime(2012, 06, 01),
new DateTime(2012, 06, 02),
//..etc
};
var startDate = new DateTime(2012, 06, 01);
var endDate = new DateTime(2012, 06, 15);
var dateRange =
Enumerable.Range(0, (int)(endDate - startDate).TotalDays + 1)
.Select(i => startDate.AddDays(i));
var datesNotMatched = dateRange.Except(dateList);
Upvotes: 2