从LINQ查询返回多个流

时间:2020-12-28 00:08:58

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

我想写一个返回两个对象流的LINQ查询。在F#中,我会编写一个Seq表达式,它创建一个2元组的IEnumerable,然后运行Seq.unzip。在C#(在.NET 3.5上)执行此操作的正确机制是什么?

Cheers, Jurgen

2 个解决方案

#1


Your best bet is probably to create a Pair<T1, T2> type and return a sequence of that. (Or use an anonymous type to do the same thing.)

你最好的选择可能是创建一个Pair 类型并返回一个序列。 (或使用匿名类型执行相同的操作。) ,t2>

You can then "unzip" it with:

然后你可以用以下方式“解压缩”它:

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

It's probably worth materializing pairs first though (e.g. call ToList() at the end of your first query) to avoid evaluating the query twice.

首先可能值得实现对(例如,在第一次查询结束时调用ToList())以避免两次评估查询。

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

基本上这与您的F#方法完全相同,但没有内置支持。

#2


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

由于C#中缺少元组,您可能会创建一个匿名类型。语义是这样的:

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

This way you're not bound in the amount of streams you return, you can just add a field to the anonymous type pretty like you can add an element to a tuple.

这样你就不会受到返回的数量的束缚,你只需要向匿名类型添加一个字段,就像你可以向元组添加一个元素一样。

#1


Your best bet is probably to create a Pair<T1, T2> type and return a sequence of that. (Or use an anonymous type to do the same thing.)

你最好的选择可能是创建一个Pair 类型并返回一个序列。 (或使用匿名类型执行相同的操作。) ,t2>

You can then "unzip" it with:

然后你可以用以下方式“解压缩”它:

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

It's probably worth materializing pairs first though (e.g. call ToList() at the end of your first query) to avoid evaluating the query twice.

首先可能值得实现对(例如,在第一次查询结束时调用ToList())以避免两次评估查询。

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

基本上这与您的F#方法完全相同,但没有内置支持。

#2


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

由于C#中缺少元组,您可能会创建一个匿名类型。语义是这样的:

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

This way you're not bound in the amount of streams you return, you can just add a field to the anonymous type pretty like you can add an element to a tuple.

这样你就不会受到返回的数量的束缚,你只需要向匿名类型添加一个字段,就像你可以向元组添加一个元素一样。