1

I'm trying to convert an IEnumerable<Tuple<TParent, TChild>> to an IEnumerable<TParent> knowing that TParent object contains a ICollection<TChild>.

I started this way but I really don't now how to normalize the results :

for (int i = 0; i < resultEnumerable.Count(); i++)
{
    var parent = resultEnumerable.ElementAt(i).Item1;
    var child = resultEnumerable.ElementAt(i).Item2;.........
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
user3574857
  • 377
  • 4
  • 16
  • 1
    `myEnumerable.Select(tupleItem => tupleItem.Item1)`? – poke Jul 26 '17 at 22:16
  • 2
    Good edit @JasonBoyd - beat me to it; to OP (user3574857) - for next time, you might want to [look here](https://stackoverflow.com/posts/45338306/edit) to see how the "markdown" is done for inline code - it is easier than it sometimes looks! – Marc Gravell Jul 26 '17 at 22:18
  • Not good enough though @Jason haha – DavidG Jul 26 '17 at 22:21
  • 3
    unrelated point: it is a **really, really** bad idea to do `i < resultEnumerable.Count()` - depending on what `resultEnumerable` is, that could be iterating the sequence **once per `i`** - so you'll be doing `N+1` loops of `N` instead of 1 loop of `N`. `foreach` would be the preferred way. It should also be noted that `ElementAt(i)` has undefined performance, and can *also* be `O(i)` (which will average at `N/2` - twice, so `N`) so now we're at `N^2` performance!, compared to `N` for a simple `foreach` – Marc Gravell Jul 26 '17 at 22:23

1 Answers1

3
resultEnumerable.Select(x => x.Item1);
Jason Boyd
  • 6,839
  • 4
  • 29
  • 47