【发布时间】:2013-01-18 10:31:11
【问题描述】:
我遇到了扩展方法解析的问题。 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>)'
我在 string 上为 Jon Skeet 在 this post 制作的 MoreLINQ 找到了 Concat 方法的良好分辨率,但我不知道 zip 方法的良好分辨率。
注意:您始终可以使用静态方法调用语法,并且在
下一切正常MoreEnumerable.Zip(students, colors, (s, c) => s + c )
但有点忽略了扩展语法糖的意义。如果您使用 LINQ 和 MoreLINQ 调用进行大量数据转换 - 您不想在中间使用静态方法调用。
有没有更好的方法来解决这种歧义?
【问题讨论】:
-
MoreLINQ 没有 4.0 友好版本吗? (即放弃MoreLINQ自己的
Zip函数) -
...或者自己制作一个,它是开源的 :)
-
@AakashM 不,没有。你可以在code.google.com/p/morelinq/issues/detail?id=60 看到他们关于
zip和.NET 4.0 的问题,在gist.github.com/2869531 看到NoConflict 解决方案
标签: c# .net linq ambiguous-call morelinq