c# - 如何解决 Enumerable 和 MoreLINQ 之间不明确的 ZIP 调用?

标签 c# .net linq ambiguous-call morelinq

我遇到了扩展方法解析的问题。 LINQ 和 MoreLINQ 包含 zip 方法,它自 4.0 版本以来就存在于 .NET 中,并且始终在 MoreLINQ 中。图书馆。但是您不能使用带有旧式扩展方法语法的实现之一。所以这段代码不会编译

using MoreLinq;
using System.Linq;


var students = new [] { "Mark", "Bob", "David" };
var colors = new [] { "Pink", "Red", "Blue" };

students.Zip(colors, (s, c) => s + c );

错误:

The call is ambiguous between the following methods or properties: 
'MoreLinq.MoreEnumerable.Zip<string,string,string>
(System.Collections.Generic.IEnumerable<string>, 
System.Collections.Generic.IEnumerable<string>, System.Func<string,string,string>)' and 
'System.Linq.Enumerable.Zip<string,string,string>
(System.Collections.Generic.IEnumerable<string>, 
System.Collections.Generic.IEnumerable<string>, System.Func<string,string,string>)'

我已经为 Jon Skeet 在 this post 制作的 MoreLINQ 的 string 上的 Concat 方法找到了很好的解决方案。 ,但我不知道 zip 方法的良好解决方案。

注意:您始终可以使用静态方法调用语法,它与

一起正常工作
MoreEnumerable.Zip(students, colors, (s, c) => s + c )

但有点忽略了扩展语法糖的要点。如果您使用 LINQ 和 MoreLINQ 调用进行大量数据转换 - 您不想在中间使用静态方法调用。

有没有更好的方法来解决这种歧义?

最佳答案

使其编译的方法是:

var students = new[] { "Mark", "Bob", "David", "test" }.AsQueryable();
var colors = new[] { "Pink", "Red", "Blue" };

students
    .Zip(colors, (s, c) => s + c)
    .Dump();

students 对象必须转换为 IQueryable 对象。

关于c# - 如何解决 Enumerable 和 MoreLINQ 之间不明确的 ZIP 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14396971/

相关文章:

c# - 检查通用 IEnumerable 是否为空

c# - 如何将特定时区的午夜正确转换为 Utc 时间?

c# - 项目的位置和 'Cannot implicitly convert type to string error'

c# - 在代码中的 StringLengthAttribute 上设置 MaximumLength

c# - 使用 Entity Framework Code First 在 MySQL 中创建 VARCHAR BINARY 列

c# - 使用 .NET 检测 .ogg 文件属性? channel 数,每 channel 位数,采样率?

c# - IIS 8 : Asp. 网络 Web 应用程序部署

c# - 签署 .net 核心程序集的正确方法

c# - 如何按多个通用 linq 表达式分组

c# - 如何在 DataRow 上使用 .ToDictionary() 扩展方法