c# - How to map two lists using linq -
If I have a list of such dates:
list & lt; DateTime & gt; {5-10-2014,6-10-2014,7-10-2014}
And I have a list of such sessions:
< From the code> list & lt; Integer & gt; {1,2,3,4,5,6}
How to map sessions based on two sessions (using leaners) in the order of each date.
I want a result like this:
5-10-2014 1 5-10-2014 2 6-10-2014 3 6-10-2014 4 7 -10-2014 5 7-10-2014 6
This is a way that you With it can:
var grouped date = dates. Select ((date, index) = & gt; new {date = date, index = index}) .GroupJoin (numbers, datewise and index => index, number = & gt; (num - 1) / 2, (date With index, numbers) = & gt; new [] {new} date = date by index.date, number = number.First ()}, new {date = dateWithIndex.Date, number = nums.Last ()}}) .SelectMany (grp = & gt; grp);
Example:
How it works:
- Date list in a new order Project that contains the index and date of each date
-
group zone
with the list of numbers in that collection. To correlate both of them, use theindex
, we use the step 1 to date 1, and(num - 1) / 2
, becauseindex
is zero-based. - To create a reusult, create a new array containing two items, one for each number associated with the date.
- Finally, to adjust the call
SelectMany
sequence.
Comments
Post a Comment