I have an IEnumerable<T> that has a Created field, which is a date.
There can be multiple T‘s per date and sometimes there are no T‘s for a given date.
Currently I’m grouping these by the date, which gives me all the dates that have at least one T, and the T‘s under them.
What I want though, is something I can use as part of a query that will get me all dates within a range, regardless of whether there are any T‘s with the given date.
Current Code:
var adjustments = DAL.GetAdjustmentsInDateRange(Start, End);
from adjustment in adjustments
group adjustment by adjustment.Created.Date into adjustmentsByDay
orderby adjustmentsByDay.Key descending
select ....
Here, adjustmentsByDay doesn’t have all dates between Start and End. What I want is for it to include them, with no elements.
How can I do that?
I’ve put together a general-purpose LINQ-to-objects extension method to insert missing things into a sequence:
You’ll also need a custom implementation of
IGrouping:Then you’ll need to end your query after the
orderby, follow it with this call, and then put yourselectafterwards:This will go haywire if your keys aren’t ordered or if one of them doesn’t fall in the correct place (e.g. in your case, isn’t on midnight).
This has the advantage of not needing multiple queries on the original source to determine the min/max values in order to generate a list of keys, and then a further query to join and get the data.