score:4
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:
- project the dates list into a new sequence containing the index of each date and the date itself
groupjoin
that collection with the list of numbers. to correlate the two, use theindex
we got from step 1 for the date, and(num - 1) / 2
, sinceindex
is zero-based.- to build the reusult, create a new array containing two items, one for each number associated with the date.
- finally, call
selectmany
to flatten the sequence.
score:1
i think best way to map values in c# is to use dictionary<key,value>
. you can have a one-to-many relationship -- that is, one date has several sessions. the code would look like this:
dictionary<datetime, list<int>> dicdatesesstion = new dictionary<datetime, list<int>>();
score:2
assumed that listi.length == 2*listd.length
list<int> listi = new int[] { 1, 2, 3, 4, 5, 6 }.tolist();
list<datetime> listd = new datetime[] { datetime.parse("5-10-2014"), datetime.parse("6-10-2014"), datetime.parse("7-10-2014") }.tolist();
ienumerable<tuple<datetime, int>> result =
enumerable.range(0, listi.count)
.select(x => new tuple<datetime, int>(listd[x / 2], listi[x]));
score:2
if you develop your project with .net 4.0 or higher versions you can use zip().
ienumerable<int> keys = new list<int>() { 1, 2, 3, 4, 5, 6 }; // your keys
ienumerable<datetime> values = new list<datetime>() { datetime.parse("5 - 10 - 2014"), datetime.parse("6 - 10 - 2014"), datetime.parse("7 - 10 - 2014") }; // your values
var mappeddictionary = keys.zip(values, (k, v) => new {
k,
v
}).todictionary(x => x.k, x => x.v);
this link proves how it works.. https://dotnetfiddle.net/ykyf8s
score:4
here's a simple solution:
dates
is a list of datetime
and sessions is a list of int
.
var result = sessions.select((session,i) =>
new{session = session, date = dates[i/2]});
the result contains an ienumerable of anonymous objects, you can access its properties like this:
foreach(var sessiondate in result)
{
var date = sessiondate.date;
var session = sessiondate.session.
}
Source: stackoverflow.com
Related Query
- How to map two lists using linq
- Using Linq and C#, how could I kinda merge two lists over some criteria?
- How to efficiently select certain items from two lists using LINQ
- How to join two lists of parent/child objects using LINQ while grouping on child key
- How to create a Dictionary out of two separate lists using linq and lambda expressions
- Using linq c# how can I select using a condition two different lists
- How to merge two lists while adding metadata indicating value source with LINQ statement?
- How to match specific properties within two lists in c# using LINQ
- How to merge two lists using LINQ Suming a especific field when the id matched
- How to compare two c# lists data equal using linq
- How to merge two lists using LINQ Suming especifics fields with sublists
- Compare Two Lists Via One Property Using LINQ
- Get the difference between two lists using LINQ
- How to merge two lists using LINQ?
- C# How to split a List in two using LINQ
- How do I add two lists in Linq so addedList[x] = listOne[x] + listTwo[x]?
- LINQ Query - How to map a resultset into another object using Select
- Filter two lists on one property c# using linq
- How to reuse a linq expression for 'Where' when using multiple source tables
- Using Linq Except with two lists of int arrays
- Two approaches to Cartesian product of a collection of lists using LINQ
- How to sum Timespan of subtraction between two datetimes in Linq when using group by?
- Using Linq and C#, trying to get two lists from a list of master items grouped by two inner lists
- Combine entries from two lists by position using LINQ
- How to convert list of objects with two fields to array with one of them using LINQ
- How can I combine this code into one or two LINQ queries?
- Intersection of two lists using LINQ
- Using Linq and C#, is it possible to join two lists but with interleaving at each item?
- How can I write the following code more elegantly using LINQ query syntax?
- Comparing two lists using linq to sql
More Query from same tag
- linq to sql c#: replace a foreign key with its string value and keep the table structure
- How to convert the return type of the model data from Product entity to ProductViewModel objects using LINQ select?
- Migration from NHibernate to Entity Framework 4.1?
- ServiceStack Linq merge fields and partial update
- Entity Framework - Results from subquery, speed issue?
- LINQ to Entity Any() with related Object Collection
- Implementing a custom QueryProvider with in-memory query
- Can a method chain be called LINQ?
- Get the number of hours between time Period
- Stumped in Dependency Property in VB
- Use LINQ without putting data in memory
- C# Visual Studio: How do I update a DB from Edit with DataGridView using LINQ?
- How can you convert a List<X> to Dictionary<X,Y> filling the value with null?
- DbContext is just schema or schema and records of my db in memory
- Why do I need a ToList() to avoid disposed context errors?
- Asp.netMvc join two tables with linq query
- Linq group by question
- Linq query with two many-many relationships
- LINQ: How to dynamically use an ORDER BY in linq but only if a variable is not string.empty or null
- NHibernate uses wrong column type for LINQ contains query (varchar to nvarchar)
- Using LINQ to parse the numbers from a string
- Telerik RadGridView won't display Dates
- Linq to EF query with criteria filter
- how to LINQ query show repeated records That 2 or less than twice repeated
- Accessing info from FK-related tables with LINQ
- How to create C# Reflection to Lambda Expression returning boolean
- How do i trouble shoot a failed ajax return
- LINQ: Get all objects with a matching string property from an incoming IEnumerable<string> in a performant way
- C#/.NET/WPF: Obtaining subsets from LINQ, rather than the IEnumerable
- Nested query in entity framework