If i have a List of dates like this :
List<DateTime> {5-10-2014,6-10-2014,7-10-2014}
and i have list of sessions like this :
List<int> {1,2,3,4,5,6}
How to map the sessions to dates considering that each date has two sessions in order(using linq).
i want the result like that :
5-10-2014 1
5-10-2014 2
6-10-2014 3
6-10-2014 4
7-10-2014 5
7-10-2014 6
Here's one way you could do it with GroupJoin
:
var groupedDates = dates
.Select((date, index) => new { Date = date, Index = index })
.GroupJoin(
numbers,
dateWithIndex => dateWithIndex.Index,
num => (num - 1) / 2,
(dateWithIndex, nums) => new[]
{
new { Date = dateWithIndex.Date, Number = nums.First() },
new { Date = dateWithIndex.Date, Number = nums.Last() }
})
.SelectMany(grp => grp);
Example: https://dotnetfiddle.net/2IIKhj
Here's how this works:
GroupJoin
that collection with the list of numbers. To correlate the two, use the Index
we got from step 1 for the date, and (num - 1) / 2
, since Index
is zero-based.SelectMany
to flatten the sequence.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With