我遇到了扩展方法解析的问题。 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/