Ily*_*nov 11 .net c# 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 );
Run Code Online (Sandbox Code Playgroud)
错误:
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>)'
Run Code Online (Sandbox Code Playgroud)
我已经找到了Jon Skeet在这篇文章中为MoreLINQ制作Concat方法的好分辨率,但是我不知道方法的好分辨率.stringzip
注意:您始终可以使用静态方法调用语法,并且一切正常
MoreEnumerable.Zip(students, colors, (s, c) => s + c )
Run Code Online (Sandbox Code Playgroud)
但是错过了扩展语法糖点的一点点.如果您使用LINQ和MoreLINQ调用进行大量数据转换 - 您不希望在中间使用静态方法调用.
有没有更好的方法来解决这种歧义?
您可以使用相同的方法创建一个包装类,但名称不同。这有点脏,但如果你真的喜欢扩展语法,这是唯一的方法。
public static class MoreLinqWrapper
{
public static IEnumerable<TResult> MlZip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
{
return MoreLinq.Zip(first, second, resultSelector);
}
}
Run Code Online (Sandbox Code Playgroud)
使其编译的一种方法是:
var students = new[] { "Mark", "Bob", "David", "test" }.AsQueryable();
var colors = new[] { "Pink", "Red", "Blue" };
students
.Zip(colors, (s, c) => s + c)
.Dump();
Run Code Online (Sandbox Code Playgroud)
students必须将对象转换为对象IQueryable。
| 归档时间: |
|
| 查看次数: |
1746 次 |
| 最近记录: |