加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何解决Enumerable和MoreLINQ之间模糊的ZIP调用?

发布时间:2020-12-15 06:25:43 所属栏目:百科 来源:网络整理
导读:我遇到了扩展方法解决问题. LINQ和MoreLINQ包含zip方法,它自4.0版本以来一直存在于.NET中,并始终在 MoreLINQ库中.但是您不能使用其中一个实现与旧的扩展方法语法.所以这段代码不会编译 using MoreLinq;using System.Linq;var students = new [] { "Mark","Bo
我遇到了扩展方法解决问题. 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>)' and 
'System.Linq.Enumerable.Zip<string,string>)'

我在78001发布了Jon Skeet的MoreLINQ的字符串,为Concat方法找到了很好的解决方案,但是我并没有意识到zip方法的很好的分辨率.

注意:您可以随时使用静态方法调用语法,一切正常

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

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

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

解决方法

编译方式是:
var students = new[] { "Mark","David","test" }.AsQueryable();
var colors = new[] { "Pink","Blue" };

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

学生对象必须转换为IQueryable对象.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读