Return multiple threads from a LINQ query

I want to write a LINQ query that returns two streams of objects. In F #, I would write a Seq expression that creates an IEnumerable from 2 tuples and then runs Seq.unzip. What is the proper mechanism for this in C # (on .NET 3.5)?

Greetings, Jurgen

+4
source share
2 answers

It is best to create a type Pair<T1, T2> and return its sequence. (Or use an anonymous type to do the same.)

You can "unzip" it with:

 var firstElements = pairs.Select(pair => pair.First); var secondElements = pairs.Select(pair => pair.Second); 

You should probably materialize pairs first, though (for example, call ToList() at the end of your first request) to avoid evaluating the request twice.

Basically, this is exactly the same as your F # approach, but without built-in support.

+3
source

Due to the lack of tuples in C #, you can create an anonymous type. Semantics for this:

 someEnumerable.Select( inst => new { AnonTypeFirstStream = inst.FieldA, AnonTypeSecondStream = inst.FieldB }); 

That way, you are not tied to the number of threads returned, you can simply add the field to the anonymous type, as if you could add an element to the tuple.

+2
source

Source: https://habr.com/ru/post/1285959/


All Articles