2
var results = from thing in AnArrayOfThings
              let list = Function(thing)
              ???
              select OneLongResultsList;

How can I merge all the "list" collections into one long list? I'm new to linq and I can't use lambda expressions.

Basically, what I want to do, is this:

List<Result> results = new ...;
foreach (Thing t in ListOfThings)
{
    List<Result> list = Function( t );
    results.MergeOrAdd( list );
}
  • Is two list are same type? Try some tutorials. Concat , Union are some of methods. https://stackoverflow.com/questions/720609/create-a-list-from-two-object-lists-with-linq – Eldho May 20 '18 at 08:50
  • [Merge two (or more) lists into one, in C# .NET](https://stackoverflow.com/a/4488061/1417185) – Paritosh May 20 '18 at 08:52
  • Yes, they are the same type. Concat works with two lists, which I don't know how to adapt to multiple, like in a foreach loop –  May 20 '18 at 08:53

1 Answers1

3

Maybe you just need SelectMany?

var results = AnArrayOfThings.SelectMany(x => Function(x));

Or in query syntax:

var results = from thing in AnArrayOfThings
              from thingInAList in Function(thing)
              select thingInAList;
Sweeper
  • 213,210
  • 22
  • 193
  • 313