3
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 );
}
3
  • Is two list are same type? Try some tutorials. Concat , Union are some of methods. stackoverflow.com/questions/720609/… Commented May 20, 2018 at 8:50
  • Merge two (or more) lists into one, in C# .NET Commented May 20, 2018 at 8: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 Commented May 20, 2018 at 8:53

1 Answer 1

4

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;
Sign up to request clarification or add additional context in comments.

2 Comments

ooh so this gets all the "thingInAList" ...things of all the lists??
@Herpn'nFoDerp yes it's like a flatMap function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.