0

I would like to Zip multiple List of integer and don't know how to do that in LinQ.

Here's my List :

List<KeyValuePair<Guid, List<int>>> totals = Totals.Where(x => x.Key == myGuid).ToList();
//where Totals is a List<KeyValuePair<Guid, List<int>>>

List<List<int>> totalsValue = totals.Select(s => s.Value).ToList();
//I want to Zip all my List<int> in totalsValue and put it into listToReturn


List<int> listToReturn = new List<int>();

I want something like that : http://linqsamples.com/linq-to-objects/other/Zip-lambda-csharp but in this exemple, lists are separeted. Mine is a List> Where listToReturn represents the list to return.

Can someone help me ?

Result : Where adventest is a List and agiltest also enter image description here

  • No Totals is a List>> – Stefano Martines Dec 05 '17 at 15:14
  • It's unclear from your question exactly what you are trying to return. Do you want a flat list of all of the integers contained within the `Value` property of the `KeyValuePair>`s? Or do you want a list of the total values in each of the `List`s? Or something else? – Donut Dec 05 '17 at 15:14
  • You haven't understood what `Enumerable.Zip` does. It links two sequences by index. You want to flatten all lists, that's `Enumerable.SelectMany`. – Tim Schmelter Dec 05 '17 at 15:14
  • I want something like that: http://linqsamples.com/linq-to-objects/other/Zip-lambda-csharp But in this exemple, lists are separated. In my case I have a List> – Stefano Martines Dec 05 '17 at 15:20
  • @StefanoMartines That page is just a generic description of the `Zip` function - can you be more specific as to what (specifically) you are trying to accomplish? It seems like `Zip` is probably the wrong solution, which is why people are suggesting `SelectMany`. However, even that may not be appropriate - we need some more clarity to understand what you're trying to do. – Donut Dec 05 '17 at 15:22
  • I've update my description. – Stefano Martines Dec 05 '17 at 15:35
  • @GertArnoldf What's your basis for asserting that the duplicate isn't correct? – Servy Dec 05 '17 at 15:42
  • @Servy Well, I explained, because of the return type. What OP wants isn't related to Zip, it's only that the output in the example looks like what they're after. – Gert Arnold Dec 05 '17 at 15:46
  • @GertArnold It's exactly what the OP asked for. It produces their described results. The fact that some other solution produces a value with the same type as the OP's expected results, but not the correct result doesn't make that other result correct, or the question asking *for the exact thing the OP is asking for* not a duplicate. The question is *very* clear in stating they're looking for a way of zipping a list of lists, not just two lists. That you wrote something else that also produces a `List` doesn't make it a correct answer, or the question not a duplicate. – Servy Dec 05 '17 at 15:47
  • @StefanoMartines You should really use the solutions provided at [this exact duplicate](https://stackoverflow.com/questions/17976823/how-to-zip-or-rotate-a-variable-number-of-lists) question. I know several people have reopened the question so that they can have their re-posting their lower quality duplicate answers here, but there are far better versions on the canonical version of the question. – Servy Dec 05 '17 at 17:09

1 Answers1

0

I think you are looking for SelectMany:

List<int> listToReturn= totals = Totals.Where(x => x.Key == myGuid)
                                       .SelectMany(s => s.Value)
                                       .ToList();

What you are trying to do is flatten all the lists into one sequence and that is what SelectMany extension method do. Zip, in the other side, merges each element of the first sequence with an element that has the same index in the second sequence, in other words, it allows you to project from two sequences of the same length.

Update

Now I understand what you are trying to achieve:

var lists= Totals.Where(x => x.Key == myGuid).Select(e=>Value).ToList();// Select the lists
if(lists.Count>0)
  var result=  Enumerable.Range(0, lists[0].Count).Select(i=> lists.Sum(l=>l[i]));

The thing with this solution you need to make sure that all the selected lists have the same size. I'm pretty sure there is a more elegant solution but this was that come to my mind now. If I think in something better I will update my answer

ocuenca
  • 38,548
  • 11
  • 89
  • 102