Linq to Objects DateTime Except ignore time - c#

I have a 2 List<DateTime> and want to return the subset of the first list that isn't in the second list but ignore the time.
Is it possible to do this using the Except extension method?
var missingDates = dateList1.Except(dateList2);
Would I need to set the time in both lists to 00:00:00 or is there some way I can use the .Date property to compare?

Try this:
var missingDates = dateList1.Select(x=>x.Date).Except(dateList2.Select(y=>y.Date));
Though this ignores the time completely, meaning that in your result the time will be missing, too...
On a second thought: You could of course implement a custom IEqualityComparer to preserve the time in the result:
public class DateComparer : IEqualityComparer<DateTime>
{
public bool Equals(DateTime left, DateTime right)
{
return left.Date.Equals(right.Date);
}
public int GetHashCode(DateTime dateTime)
{
return dateTime.Date.GetHashCode();
}
}
And then use that comparer:
var missingDates = dateList1.Except(dateList2, new DateComparer());

This will ignore the time in the comparison but return the dates with times intact:
List<DateTime> missingDates = dateList1.Where(list1Item => !dateList2.Exists(list2Item => list2Item.Date == list1Item.Date)).ToList();

Related

DateTime substracting days doesn't work

I am using the following code in order to substract a day of the DateTime until I am getting Monday:
DateTime currentWeek = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day);
while (currentWeek.DayOfWeek.ToString() != "Monday")
{
currentWeek.AddDays(-1);
MessageBox.Show(currentWeek.Day.ToString());
MessageBox.Show(currentWeek.DayOfWeek.ToString());
}
beginDate is in the first run set to the current Date of DateTime.Now.
For me this loops forever, and the day of currentWeek always stays the same (29) even though I am substracting 1 everytime I am looping through.
I am already using another function that takes a DateTime and a bool Parameter, which does pretty much the same and works:
private void ErstenTagDerWocheAuswaehlen(DateTime date, bool anfangDerWoche = true)
{
string wochentagName;
int incrementor;
if(anfangDerWoche == true)
{
wochentagName = "Monday";
incrementor = -1;
}
else
{
wochentagName = "Friday";
incrementor = 1;
}
while(date.DayOfWeek.ToString() != wochentagName)
{
date = date.AddDays(incrementor);
}
}
Can someone explain to me why the upper code doesn't work whilst the lower one does?
You have to assign the resulting value, DateTime is immutable.
currentWeek = currentWeek.AddDays(-1);
About your 2nd question:
Use the enum for day of the week, do not try to convert a day of the week to a string for a comparison. The type is DayOfWeek.
Again, a DateTime is not mutable so you have to return a DateTime instance as you can't mutate the one that was passed in (without passing it as ref)
Code change
private DateTime ErstenTagDerWocheAuswaehlen(DateTime date, bool anfangDerWoche = true)
{
System.DayOfWeek wochentagName;
int incrementor;
if(anfangDerWoche == true)
{
wochentagName = System.DayOfWeek.Monday;
incrementor = -1;
}
else
{
wochentagName = System.DayOfWeek.Friday;
incrementor = 1;
}
while(date.DayOfWeek != wochentagName)
{
date = date.AddDays(incrementor);
}
return date;
}
DateTime is an immutable struct, so you need to store the value returned from AddDays():
var t2 = currentWeek.AddDays(-1);
Then use t2. The call to AddDays() doesn't actually change currentWeek.
As DateTime is immutable, when using the AddDays it returns a new DateTime structure with the new information and does not change the given one.
Method documentation states:
Returns a new System.DateTime that adds the specified number of days to the value of this instance.
You must assign it to a variable:
currentWeek = currentWeek.AddDays(-1);

How to find nearest DateTime keys of DateTime in SortedDictionary?

I have a SortedDictionary of DateTime and double _historicalValues, and a method HistoricalValue which is passed a DateTime date. HistoricalValue must return the corresponding double for the DateTime in _historicalValues that is closest to date. Is there a nice way in which I can leverage a SortedDictionary in order to find the closest DateTime?
I think there is a better way to approach this task as my approach is something that doesn't leverage a SortedDictionary at all, and tediously has to iterate over the whole collection to evaluate the date differences first.
private readonly SortedDictionary<DateTime, double> _historicalValues;
public double HistoricalValue(DateTime date)
{
if (_historicalValues.ContainsKey(date))
return _historicalValues[date];
var closestDate = _historicalValues.Keys.OrderBy(
t => Math.Abs((t - date).Ticks)).First();
return _historicalValues[closestDate];
}
I would use the MinBy mentioned in this answer: enter link description here
And then use it like this:
return _historicalValues.MinBy(kvp => Math.Abs((kvp.Key - date).Ticks)).Value;

Find the closest DateTime key in Dictionary<DateTime, double>

I have the Date portion of DateTime as lookup value and like to retrieve the matching value in a Dictionary of type Dictionary<DateTime, double>. Note the DateTime keys are stored as only the Date portion.
My problem is that there may be no key that matches with my lookup value. What I then like to do is to find the nearest previous dateTime.Date key and matching value.
Now, I am aware that Dictionaries are not sorted by key. I could use a SortedDictionary but prefer to use a Dictionary for a specific reason or else switch to a List collection (can be pre-sorted). My question is, what would you recommend to do in this case: Would it be more efficient to retain the Dictionary structure and decrement my lookup value until I find a matching key? Or would it be better to use a list collection and use Linq? Each Dictionary contains around 5000 key/value pairs. Also, please note I look for a highly computationally efficient solution because the frequency of lookups is quite high (potentially many hundred thousand times (each lookup is guaranteed to be different from any previous value)
Since you need it fast, I think the best thing would be to use the results of a BinarySearch. This requires a List<T> that's sorted.
int result = myList.BinarySearch(targetDate);
if (result >= 0)
return myList[result];
else
{
int nextLarger = ~result;
// return next smaller, or if that doesn't exist, the smallest one
return myList[Math.Max(0, nextLarger - 1)];
}
It should be possible to create a class that combines a Dictionary<TKey,TValue> and a sorted List<TKey> that still serializes like a Dictionary<TKey,TValue>. The serialization might be as simple (in Json.NET) as putting a [JsonConverter(typeof(KeyValuePairConverter))] on your class.
Just for completeness and in case others read this in the future, if speed wasn't very important, you can do it more simply with something like this:
var result = myDict.Keys.Where(x => x < targetDate).Max();
I would use a custom structure and collection to store these informations:
public struct DateValue
{
public DateValue(DateTime date, double val)
: this()
{
this.Date = date;
this.Value = val;
}
public DateTime Date { get; set; }
}
Here is a possible implementation of a collection that holds all DateValues and encapsulates the logic to return the nearest. It uses List.BinarySearch to find it. If it doesn't find a direct match it uses the logic of BinarySearch to detect the nearest which is:
The index of the specified value in the specified array, if value is
found. If value is not found and value is less than one or more
elements in array, a negative number which is the bitwise complement
of the index of the first element that is larger than value. If value
is not found and value is greater than any of the elements in array, a
negative number which is the bitwise complement of (the index of the
last element plus 1).
public class DateValueCollection : List<DateValue>, IComparer<DateValue>
{
public DateValueCollection() { }
public DateValueCollection(IEnumerable<DateValue> dateValues, bool isOrdered)
{
if (isOrdered)
base.AddRange(dateValues);
else
base.AddRange(dateValues.OrderBy(dv => dv.Date));
}
public DateValue GetNearest(DateTime date)
{
if (base.Count == 0)
return default(DateValue);
DateValue dv = new DateValue(date, 0);
int index = base.BinarySearch(dv, this);
if (index >= 0)
{
return base[index];
}
// If not found, List.BinarySearch returns the complement of the index
index = ~index;
DateValue[] all;
if(index >= base.Count - 1)
{
// proposed index is last, check previous and last
all = new[] { base[base.Count - 1], base[base.Count - 2] };
}
else if(index == 0)
{
// proposed index is first, check first and second
all = new[] { base[index], base[index + 1] };
}
else
{
// return nearest DateValue from previous and this
var thisDV = base[index];
var prevDV = base[index - 1];
all = new[]{ thisDV, prevDV };
}
return all.OrderBy(x => (x.Date - date).Duration()).First();
}
public int Compare(DateValue x, DateValue y)
{
return x.Date.CompareTo(y.Date);
}
}
Quick test:
var dateVals = new[] {
new DateValue(DateTime.Today.AddDays(10), 1), new DateValue(DateTime.Today, 3), new DateValue(DateTime.Today.AddDays(4), 7)
};
var dvCollection = new DateValueCollection(dateVals, false);
DateValue nearest = dvCollection.GetNearest(DateTime.Today.AddDays(1));
why worry about optomisation prematurley
do it
THEN AND ONLY THEN if its slow then you have a problem
Measure it with a profiler
then understanding starts at which point you try other ways and profile them.
Answer is: If you do it any way at all and there isnt a performance problem you just saved time and managed to do something else useful to add value in your day.
Premature optimization is not only pointless you will usually be completely wrong about where you should be looking.

Finding missing dates from a collection

I've dates array with values below:
"07/07/2011", "08/05/2011", "09/07/2011", "12/07/2011"
Using this as input in my C# program, I need to build a new collection which will have missing dates..ie. 10/07/2011, 11/07/2011.
Is recursion the best way to achieve this?
Thanks.
Not at all. This should be a straightforward process. You have a starting date, you have an interval... You start walking the array and if the next value does not match your previous value plus the interval you insert a new value into the new array. If it does match, you copy that value.
If you need more data (metadata) about each entry then create a class that holds the date and whatever metadata you find useful (e.g. a bool like this_value_was_inserted_artificially)
Using recursion would unnecessarily complicate things.
No recursion needed. This can probably be optimized, but should do the job:
public static IEnumerable<DateTime> FindMissingDates(IEnumerable<DateTime> input)
{
// get the range of dates to check
DateTime from = input.Min();
DateTime to = input.Max();
// how many days?
int numberOfDays = to.Subtract(from).Days;
// create an IEnumerable<DateTime> for all dates in the range
IEnumerable<DateTime> allDates = Enumerable.Range(0, numberOfDays)
.Select(n => from.AddDays(n));
// return all dates, except those found in the input
return allDates.Except(input);
}
You can pull this off very nicely with Linq:
var dates=new[]{
DateTime.Parse("07/07/2011"),
DateTime.Parse("08/07/2011"),
DateTime.Parse("09/07/2011"),
DateTime.Parse("12/07/2011")};
var days=(dates.Max()-dates.Min()).Days;
var otherDays=
Enumerable
.Range(0,days)
.Select(d=>dates.Min().AddDays(d))
.Except(dates);

System.OutOfMemoryException thrown when computing date ranges

It might be a simple fix, but I can't for the life of me think of how to do this. I compute a bunch of StartDates and End Dates into a bunch of arrays of dates using this query:
this.Reserved = unit.Reservations.Where(r => r.Active.HasValue && r.Active.Value).SelectMany(r => Utilities.DateRangeToArray(r.StartDate, r.EndDate)).ToArray();
Utilities.DateRangeToArray() is defined as follows:
public static IEnumerable<DateTime> DateRangeToArray(DateTime start, DateTime end) {
DateTime curDate = start;
while (curDate <= end) {
yield return curDate;
curDate.AddDays(1);
}
}
Is there a way to make this less memory intensive?
Thanks!
Your code is broken - AddDays doesn't change the existing value, it returns a new value. You're ignoring that new value, thus creating an infinite loop.
Change your code to:
public static IEnumerable<DateTime> DateRangeToArray(DateTime start,
DateTime end) {
DateTime curDate = start;
while (curDate <= end) {
yield return curDate;
curDate = curDate.AddDays(1);
}
}
Another hint: unit testing can help you find this sort of problem long before you try to use the method in a LINQ query. I'd also change the name, given that it's not returning an array.
You're sure you don't have any reservations where r.StartDate > r.EndDate, right? If you do, you'll get an infinite loop, I think.
I assume the out of memory is when converting the result to the array. Two points:
The output will contain duplicate dates for overlapping reservations.
Perhaps Reserved should be a collection of date ranges (start,end) rather than containing every date?

Categories

Resources