I have a variable number of List-objects, of which I need to compare the values at the same indexes. The number of entries in the list is given and does not vary.
For example:
4 Lists
each 4 entries
So what I'd be looking for in this example, is 4 bools, one per index.
Getting the indexes of unmatching entries would be fine, too.
Pseudo:
bool isFirstEqual = (list1[i] == list2[i] == list3[i] == list4[i]);
But I need to do this in a fashion that's applicable for a variable number of lists. I could have 6 Lists, but also 2.
I was thinking of doing something with LINQs .Except() but am not sure how to use it with a variable number of lists.
I am struggling to find the elegant solution that I'm sure is out there.
Any help on this is appreciated.
If i understand what you mean, something like this might work
public static bool IsEqual<T>(int index, params List<T>[] ary)
{
for (var i = 1; i < ary.Length; i++)
if (!EqualityComparer<T>.Default.Equals(ary[0][index], ary[i][index]))
return false;
return true;
}
Usage
var isSecondelemEqual = IsEqual(1, list1, list2, list3,...)
Update
Basically takes a variable list of lists, and assumes you want to check the index of each list against each other.
bool AreIndexesEqual(int index, List<List<int>> lists)
{
int match = lists[0][index];
foreach(List<int> list in lists.Skip(1))
{
if (list[index] != match)
{
return false;
}
}
return true;
}
Given for example:
List<List<int>> listOfLists = new List<List<int>>
{
new List<int> { 1, 100, 2},
new List<int> { 2, 100, 3},
new List<int> { 3, 100, 4},
new List<int> { 4, 100, 5},
};
So a List<> of List<int>
the totally unreadable LINQ expression that will return a List<bool> is something like:
List<bool> result = Enumerable.Range(0, listOfLists.Count != 0 ? listOfLists[0].Count : 0)
.Select(x => listOfLists.Count <= 1 ?
true :
listOfLists.Skip(1).All(y => y[x] == listOfLists[0][x])
).ToList();
Here I want to show you that if your first solution to any problem is LINQ, then perhaps now you will have two problems.
Now... What does this linq does? We have 4 List<int>, each one with 3 elements... So 3 rows of 4 columns. We want to calculate the result "by row", so the first thing is discover the number of rows, that is listOfLists[0].Count (we put a pre-check for the case that we have 0 rows). Now we generate an index (like a for), using Enumerable.Range(0, numberofrows), like for (int i = 0; i < numberofrows; i++). For each row we see if there are 0 or 1 columns (listOfLists.Count <= 1), then the result is true, otherwise we compare all the other columns y[x] with the first column listOfLists[0][x].
With a dual for cycle it becomes clearer probably:
var result2 = new List<bool>(listOfLists.Count != 0 ? listOfLists[0].Count : 0);
// Note the use of .Capacity here. It is listOfLists.Count != 0 ? listOfLists[0].Count : 0
for (int col = 0; col < result2.Capacity; col++)
{
if (listOfLists.Count <= 1)
{
result2.Add(true);
}
else
{
bool equal = true;
for (int row = 1; row < listOfLists.Count; row++)
{
if (listOfLists[row][col] != listOfLists[0][col])
{
equal = false;
break;
}
}
result2.Add(equal);
}
}
Note that both programs can be simplified: new int[0].All(x => something) == true, so .All() on empty IEnumerable<> is true. You can remove listOfLists.Count <= 1 ? true : and if (...) { ... } else up to the else keyword, keeping only the code inside the else:
var result2 = new List<bool>(listOfLists.Count != 0 ? listOfLists[0].Count : 0);
// Note the use of .Capacity here. It is listOfLists.Count != 0 ? listOfLists[0].Count : 0
for (int col = 0; col < result2.Capacity; col++)
{
bool equal = true;
for (int row = 1; row < listOfLists.Count; row++)
{
if (listOfLists[row][col] != listOfLists[0][col])
{
equal = false;
break;
}
}
result2.Add(equal);
}
Here you go
IEnumerable<bool> CompareAll<T>(IEnumerable<IEnumerable<T>> source)
{
var lists = source.ToList();
var firstList = lists[0];
var otherLists = lists.Skip(1).ToList();
foreach(var t1 in firstList)
{
yield return otherlists.All(tn => tn.Equals(t1));
}
}
Related
In this code:
for (e = 0; e <= collection.Count - 2; e++)
{
var itm = collection.Read()
var itm_price = itm.Price
var forwards_satisfied_row = collection
.Skip(e + 1)
.SkipWhile(x => x.Price < ex_price)
.FirstOrDefault();
var backwards_satisfied_row = collection
.Reverse()
.Skip(collection.Count - e)
.SkipWhile(x => x.Price < ex_price)
.FirstOrDefault();
}
Suppose the collection contains millions of items and a Reverse() is too expensive, what would be the best way to achieve the same outcome as 'backwards_satisfied_row' ?
Edit:
For each item in the collection, it should find the first preceding item that matches the SkipWhile predicate.
For context I'm finding the distance a price extrema (minima or maxima) is from a horizontal clash with the price. This gives a 'strength' value for each Minima and Maxima, which determines the importance of it, and to help marry it up with extremas of a similar strength.
Edit 2
This chart shows the data in the reproc code below, note the dip in the middle at item #22, this item has a distance of 18.
Bear in mind this operation will be iterated millions of times.
So I'm trying not to read into memory, and to only evaluate the items needed.
When I run this on a large dataset r_ex takes 5 ms per row, whereas l_ex takes up to a second.
It might be tempting to iterate backwards and check that way, but there could be millions of previous records, being read from a binary file.
Many types of searches like Binary search wouldn't be practical here, since the values aren't ordered.
static void Main(string[] args)
{
var dict_dists = new Dictionary<Int32, Int32>();
var dict = new Dictionary<Int32, decimal> {
{1, 410},{2, 474},{3, 431},
{4, 503},{5, 461},{6, 535},
{7, 488},{8, 562},{9, 508},
{10, 582},{11, 522},{12, 593},
{13, 529},{14, 597},{15, 529},
{16, 593},{17, 522},{18, 582},
{19, 510},{20, 565},{21, 492},
{22, 544},{23, 483},{24, 557},
{25, 506},{26, 580},{27, 524},
{28, 598},{29, 537},{30, 609},
{31, 543},{32, 612},{33, 542},
{34, 607},{35, 534},{36, 594},
{37, 518},{38, 572},{39, 496},
{40, 544},{41, 469},{42, 511},
{43, 437},{44, 474},{45, 404},
{46, 462},{47, 427},{48, 485},
{49, 441},{50, 507}};
var i = 0;
for (i = 0; i <= dict.Count - 2; i++)
{
var ele = dict.ElementAt(i);
var current_time = ele.Key;
var current_price = ele.Value;
var is_maxima = current_price > dict.ElementAt(i + 1).Value;
//' If ele.Key = 23 Then here = True
var shortest_dist = Int32.MaxValue;
var l_ex = new KeyValuePair<int, decimal>();
var r_ex = new KeyValuePair<int, decimal>();
if (is_maxima)
{
l_ex = dict.Reverse().Skip(dict.Count - 1 - i + 1).SkipWhile(x => x.Value < current_price).FirstOrDefault();
r_ex = dict.Skip(i + 1).SkipWhile(x => x.Value < current_price).FirstOrDefault();
}
else
{ // 'Is Minima
l_ex = dict.Reverse().Skip(dict.Count - 1 - i + 1).SkipWhile(x => x.Value > current_price).FirstOrDefault();
r_ex = dict.Skip(i + 1).SkipWhile(x => x.Value > current_price).FirstOrDefault();
}
if (l_ex.Key > 0)
{
var l_dist = (current_time - l_ex.Key);
if ( l_dist < shortest_dist ) {
shortest_dist = l_dist;
};
}
if (r_ex.Key > 0)
{
var r_dist = (r_ex.Key - current_time);
if ( r_dist < shortest_dist ) {
shortest_dist = r_dist;
};
}
dict_dists.Add(current_time, shortest_dist);
}
var dist = dict_dists[23];
}
Edit: As a workaround I'm writing a reversed temp file for the left-seekers.
for (i = file.count - 1; i >= 0; i += -1)
{
file.SetPointerToItem(i);
temp_file.Write(file.Read());
}
You could make it more efficient by selecting the precedent of each item in one pass. Lets make an extension method for enumerables that selects a precedent for each element:
public static IEnumerable<T> SelectPrecedent<T>(this IEnumerable<T> source,
Func<T, bool> selector)
{
T selectedPrecedent = default;
foreach (var item in source)
{
if (selector(item)) selectedPrecedent = item;
yield return selectedPrecedent;
}
}
You could then use this method, and select the precedent and the subsequent of each element by doing only two Reverse operations in total:
var precedentArray = collection.SelectPrecedent(x => x.Price < ex_price).ToArray();
var subsequentArray = collection.Reverse()
.SelectPrecedent(x => x.Price < ex_price).Reverse().ToArray();
for (int i = 0; i < collection.Count; i++)
{
var current = collection[i];
var precedent = precedentArray[i];
var subsequent = subsequentArray[i];
// Do something with the current, precedent and subsequent
}
No need to do .Reverse() and then FirstOrDefault(), just use LastOrDefault(). Instead of Skip(collection.Count - e) use .Take(e) elements
var backwards_satisfied_row = collection
.SkipWhile(x => x.Price < ex_price) //Skip till x.Price < ex_price
.Skip(e+1) //Skip first e+1 elements
.LastOrDefault(); //Get Last or default value
You can make your code more efficient by storing collection and then just get FirstOrDefault() and LastOrDefault() for forwards_satisfied_row and backwards_satisfied_row respectively.
like,
for (e = 0; e <= collection.Count - 2; e++)
{
var itm = collection.Read()
var itm_price = itm.Price
var satisfied_rows = collection
.SkipWhile(x => x.Price < ex_price)
.Skip(e + 1)
.ToList();
var forwards_satisfied_row = satisfied_rows.FirstOrDefault();
var backwards_satisfied_row = satisfied_rows.LastOrDefault();
}
Is there a way to detect similar elements within multidimensional arrays? For example:
int[][] arrayA = {{1 , 2}, {4 , 6}, {3, 7}};
int[][] arrayB = {{3 , 2}, {1 , 2}, {8, 5}};
Both arrayA and arrayB have the element {1 , 2}. (Or simply, any element in common) Is there a way to detect that it's true?
Yes, you just need to code the logic and you can detect.
bool result = false;
foreach (var arrayAItem in arrayA)
{
foreach (var arrayBItem in arrayB)
{
if (arrayAItem.SequenceEqual(arrayBItem))
{
result = true;
break;
}
}
if (result == true)
{
break;
}
}
and a one liner
bool result = arrayA.Any(arrayAItem => arrayB.Any(arrayBItem => arrayAItem.SequenceEqual(arrayBItem)));
It's hard to give you the answer without just feeding you code, but:
For each element in Array A, check if it's equal to each element in Array B. So basically, you'd need a nested foreach. If you want to record which elements they have in common, add them to a List<ElementType> (not an array since you want to dynamically populate it).
It's not quite that simple since your arrays are nested. You can't just check arrayA[x] == arrayB[x] because you'd be comparing arrays, which are reference types (it would return false unless they both pointed to the same piece of memory). Instead, you'll have to add one more loop inside of the two foreaches you already have to check each int in arrayA against the correlating one in arrayB. int is a value type, so an == comparison would behave how you'd expect it to.
Here is how you can do this (and not only):
var commonItems = from innerA in arrayA
from innerB in arrayB
where innerA.SequenceEqual(innerB)
select innerA;
bool anyCommon = commonItems.Any();
var firstCommon = commonItems.FirstOrDefault();
var commonCount = commonItems.Count();
I guess the variable names are self explanatory :-)
Something along this?
arrayA.Where(arr => arrayB.Any(arr2 => arr1.OrderBy(x => x)
.SequenceEquals(arr.OrderBy(x => x)))).ToArray()
You can remove the two OrderBy calls if you want, depending on how you wish to consider two arrays "the same".
The following code becomes lengthened, but will give you the output you want:
private void TestArrays()
{
int[,] arrayA = new[,] { { 1, 2 }, { 4, 6 }, { 3, 7 } };
int[,] arrayB = new[,] { { 3, 2 }, { 1, 2 }, { 8, 5 } };
int parentLengthA = arrayA.GetLength(0);
int childLengthA = arrayA.GetLength(1);
int parentLengthB = arrayB.GetLength(0);
int childLengthB = arrayB.GetLength(1);
int[] itemsOfA;
int[] itemsOfB;
List<int[]> matchedArrays = new List<int[]>();
for (int i = 0; i < parentLengthA; i++)
{
itemsOfA = new int[childLengthA];
for (int j = 0; j < parentLengthB; j++)
{
itemsOfB = new int[childLengthB];
bool isMatched = true;
if (itemsOfA.Length != itemsOfB.Length)
{
isMatched = false;
break;
}
for (int k = 0; k < itemsOfA.Length; k++)
{
if (arrayA[i, k] != arrayB[j, k])
{
isMatched = false;
break;
}
else
{
itemsOfA[k] = arrayA[i, k];
}
}
if (isMatched)
{
matchedArrays.Add(itemsOfA);
}
}
}
//Just to output the matched array(s)
if (matchedArrays.Count > 0)
{
StringBuilder sb = new StringBuilder();
foreach (int[] matchedArray in matchedArrays)
{
foreach (int i in matchedArray)
{
sb.Append(i + ",");
}
sb.AppendLine();
}
MessageBox.Show(sb.ToString());
}
}
i have many lists which contain 1 or more objects, and if there is more then 1 object in the list, it should do something. When there is only 1 object in the list, then take the second list and do the same. This should happen for 4 lists.
This is my Solution, but i think this is a bad solution. Is there a far better way on handling this code?
if (list1.Count > 1)
for (int i = 0; i < list1.Count; i++)
{
DoSomething(list1);
}
else
{
if (list2.Count > 1)
for (int i = 0; i < list2.Count; i++)
{
DoSomething(list2);
}
else
{
if (list3.Count > 1)
for (int i = 0; i < list3.Count; i++)
{
DoSomething(list3);
}
else
{
...
Many thanks and best regards,
Joerg
// using System.Collections.Generic;
// using System.Linq;
// create an enumerable "list" of your lists (however many there are):
var lists = new[] { list1, list2, list3, … };
// find the first list that has more than 1 element:
var list = lists.FirstOrDefault(_ => _.Count() > 1);
// if there is such a list…
if (list != null)
{
// … then `DoSomething` to each of its items:
foreach (var item in list)
{
DoSomething(item);
}
}
I tried to use same psuedo-code style as OP. The logic replicates the original, but in condensed form and with advantage of easily adding lists to the master list array (as suggested by #Benjamin).
It is assumed that DoSomething is a single function applied to the first list which has Count > 1. If you wanted to apply a custom function per list, you could create another array for holding the function to call for the corresponding list.
listArray = new[] {list1, list2, list3, list4}; // you can add more as needed to this master array
for (int i = 0; i < listArray.Count; i++)
{
if (listArray[i].Count > 1)
{
for (int i = 0; i < listArray.Count; i++)
{
DoSomething(listArray[i]);
}
break; // http://msdn.microsoft.com/en-us/library/adbctzc4.aspx
}
}
You can check first which list you want to use:
var lists = new[]{ list1, list2, list3, list4 };
var list = lists.FirstOrDefault(l => l.Count > 1);
if(list != null)
{
list.ForEach(l => DoSomething(list));
}
Seems like you need the first list for which Count > 1.
Also, inside the for-loop, you're saying DoSomething(list1);. I think it should be DoSomething(list1[i]);
var lists = new [] { list1, list2, list3, list4 };
var target = lists.FirstOrDefault(l => l.Count() > 1);
if (target != null) {
for (int i = 0; i < target.Count; i++)
{
DoSomething(target[i]);
}
} else {
//...
}
How can I determine if List A contains all of the elements from List B in the same order?
List A can have additional elements that List B does not have, but must contain all elements of List B in the order that List B has them.
Example 1 (List A ending with ..., 4, 0, 6):
List A: List B:
5 2
9 3
2 4
3
4
0
6
This should return True.
Example 2 (List A ending with ..., 0, 4, 6):
List A: List B:
5 2
9 3
2 4
3
0
4
6
This should return False.
I found this answer from JonSkeet to see if List A contains all elements from List B however, that does not require them to be in the same order.
This takes each part of ListA and compares it with ListB with SequenceEqual:
bool containsSameSequence = ListA
.Where((item, index) => index <= ListA.Count - ListB.Count)
.Select((item, index) => ListA.Skip(index).Take(ListB.Count))
.Any(part => part.SequenceEqual(ListB));
Demo
It returns true on the first matching sequence.
Here's a quick way:
var equal = listA.Count - listB.Count < 0
? false
: Enumerable.Range(0, listA.Count - listB.Count).Any(i =>
listA.Skip(i).Take(listB.Count).SequenceEqual(listB));
However, I'd prefer to use an extension method like this:
public static bool ContainsSequence<T>(this IEnumerable<T> outer,
IEnumerable<T> inner)
{
var innerCount = inner.Count();
for(int i = 0; i < outer.Count() - innerCount; i++)
{
if(outer.Skip(i).Take(innerCount).SequenceEqual(inner))
return true;
}
return false;
}
which you can call like:
var equals = listA.ContainsSequence(listB);
And here's a more efficient version of the same extension method specific to List<T>:
public static bool ContainsSequence<T>(this List<T> outer, List<T> inner)
{
var innerCount = inner.Count;
for (int i = 0; i < outer.Count - innerCount; i++)
{
bool isMatch = true;
for (int x = 0; x < innerCount; x++)
{
if (!outer[i + x].Equals(inner[x]))
{
isMatch = false;
break;
}
}
if (isMatch) return true;
}
return false;
}
Use String.Join() to concat ListA elements together. Then use it again to concat ListB elements together. Then use ConcatListA.IndexOf(ConcatListB).
I think my version is more efficient.
public static class CollectionExtension
{
public static bool SequenceContain<T>(this IEnumerable<T> target, IEnumerable<T> that)
{
var targetEnumerater = target.GetEnumerator();
var thatEnumerater = that.GetEnumerator();
var thatHasValue = thatEnumerater.MoveNext();
var targetHasValue = targetEnumerater.MoveNext();
var matchCount = 0;
try
{
while (thatHasValue && targetHasValue)
{
if (!EqualityComparer<T>.Default.Equals(targetEnumerater.Current, thatEnumerater.Current))
{
if (matchCount > 0)
{
thatEnumerater.Reset();
thatEnumerater.MoveNext();
matchCount = 0;
}
targetHasValue = targetEnumerater.MoveNext();
continue;
}
targetHasValue = targetEnumerater.MoveNext();
thatHasValue = thatEnumerater.MoveNext();
matchCount++;
}
return matchCount == that.Count();
}
finally
{
thatEnumerater.Dispose();
targetEnumerater.Dispose();
}
}
}
Anyone know of a way to add a value to a range of generic lists in c#?
I'm currently building up a large List<List<int>> and the whole process is taking too long and I'm trying to avoid using foreach loops and nested foreach loops in order to shave some time off.
Lets say I had 600 rows in a generic list. For each of the first 200 rows, I'd like to add a "1". For the next 200, I'd like to add a "2". For the next 200, I'd like to add a "3".
The way I'm doing that now, I have to loop through it 600 times and add each one individually, whereas what I'd like to do is loop through it 3 times and add the entries in bulk.
The code I was hoping for would be something like:
List<List<int>> idList = GetFullList(); //list contains 600 rows
int[] newItems = {1, 3, 5};
int count = 0;
int amountToAmend = 200;
foreach (int i in newItems)
{
//List<int> newID = new List<int>();
//newID.Add(i);
(idList.GetRange(count, amountToAmend)).Add(i);
count += amountToAmend;
}
Obviously this doesn't work, but hopefully you can see the kind of thing I'm going for. In my application I'm currently needing to do tens of thousands of unnecessary loops, when often less than 10 could feasibly do the job if the code exists!
UPDATE: I'm not sure I've explained this well, so just to clarify, here are the results I'm looking for here
If I have a list with 6 rows like so:
[6,7,8]
[5,6,7]
[6,4,8]
[2,4,7]
[5,1,7]
[9,3,5]
i know that I'd like to add a 1 to the first 3 rows and a 2 to the next 3 rows, so they would become:
[6,7,8,1]
[5,6,7,1]
[6,4,8,1]
[2,4,7,2]
[5,1,7,2]
[9,3,5,2]
This is easy to do with foreach loops and is how I currently do it, but because of the sheer volume of data involved, I'm looking for ways to cut the time taken on specific functions. I'm not sure if a way exists tbh, but if anyone knows, then it'll be the good people of Stack Overflow :)
You may use Skip and Take methods from LINQ.
like idList.Skip(0).Take(200) it will give you first 200 items from your list, then you may update these items.
For update you may say:
int increment=2;
list.Select(intVal=> intVal+increment).ToList();
How about this:
foreach (int i in newItems)
{
foreach (var row in idList.Skip(count).Take(amountToAmend))
{
row.Add(i);
}
count += amountToAmend;
}
Or with a for-loop:
foreach (int i in newItems)
{
for (int j = 0; j < amountToAmend; j++)
{
idList[count + j].Add(i);
}
count += amountToAmend;
}
You want to have amountToAmend times each item in newItems ?
Like :
200 times 1
200 times 3
200 times 5
If so, you can try :
int amountToAmend = 200;
List<int> newItems = new List<int>(){ 1, 3, 5 };
<List<int>> idList = new List<List<int>>();
newItems.ForEach(i => idList.Add(new List<int>(Enumerable.Repeat(i, amountToAmend))));
List<List<int>> idList = GetFullList(); //list contains 600 rows
var iterator = idList.Begin();
int[] newItems = {1, 3, 5};
int count = 0;
int amountToAmend = 200;
foreach (var item in newItems)
{
iterator = iterator.AddItem(item);
iterator = iterator.MoveForward(amountToAmend);
}
public struct NestedListIterator<T>
{
public NestedListIterator(List<List<T>> lists, int listIndex, int itemIndex)
{
this.lists = lists;
this.ListIndex = listIndex;
this.ItemIndex = itemIndex;
}
public readonly int ListIndex;
public readonly int ItemIndex;
public readonly List<List<T>> lists;
public NestedListIterator<T> AddItem(T item)
{
var list = lists.ElementAtOrDefault(ListIndex);
if (list == null || list.Count < ItemIndex)
return this;//or throw new Exception(...)
list.Insert(ItemIndex, item);
return new NestedListIterator<T>(this.lists, this.ListIndex, this.ItemIndex + 1);
}
public NestedListIterator<T> MoveForward(List<List<T>> lists, int index)
{
//if (index < 0) throw new Exception(..)
var listIndex = this.ListIndex;
var itemIndex = this.ItemIndex + index;
for (; ; )
{
var list = lists.ElementAtOrDefault(ListIndex);
if (list == null)
return new NestedListIterator<T>(lists, listIndex, itemIndex);//or throw new Exception(...)
if (itemIndex <= list.Count)
return new NestedListIterator<T>(lists, listIndex, itemIndex);
itemIndex -= list.Count;
listIndex++;
}
}
public static int Compare(NestedListIterator<T> left, NestedListIterator<T> right)
{
var cmp = left.ListIndex.CompareTo(right.ListIndex);
if (cmp != 0)
return cmp;
return left.ItemIndex.CompareTo(right.ItemIndex);
}
public static bool operator <(NestedListIterator<T> left, NestedListIterator<T> right)
{
return Compare(left, right) < 0;
}
public static bool operator >(NestedListIterator<T> left, NestedListIterator<T> right)
{
return Compare(left, right) > 0;
}
}
public static class NestedListIteratorExtension
{
public static NestedListIterator<T> Begin<T>(this List<List<T>> lists)
{
return new NestedListIterator<T>(lists, 0, 0);
}
public static NestedListIterator<T> End<T>(this List<List<T>> lists)
{
return new NestedListIterator<T>(lists, lists.Count, 0);
}
}
There is no builtin function, although you cannot avoid looping(explicit or implicit) at all since you want to add a new element to every list.
You could combine List.GetRange with List.ForEach:
var newItems = new[] { 1, 2 };
int numPerGroup = (int)(idList.Count / newItems.Length);
for (int i = 0; i < newItems.Length; i++)
idList.GetRange(i * numPerGroup, numPerGroup)
.ForEach(l => l.Add(newItems[i]));
Note that above is not Linq and would work even in .NET 2.0
This is my old approach which was not what you needed:
You can use Linq and Enumerable.GroupBy to redistribute a flat list into nested lists:
int amountToAmend = 200;
// create sample data with 600 integers
List<int> flattened = Enumerable.Range(1, 600).ToList();
// group these 600 numbers into 3 nested lists with each 200 integers
List<List<int>> unflattened = flattened
.Select((i, index) => new { i, index })
.GroupBy(x => x.index / amountToAmend)
.Select(g => g.Select(x => x.i).ToList())
.ToList();
Here's the demo: http://ideone.com/LlEe2