简体   繁体   English

.net6 & MoreLinq : System.Linq.Enumerable.DistinctBy 和 MoreLinq.MoreEnumerable.DistinctBy 之间的调用不明确

[英].net6 & MoreLinq : Call is ambiguous between System.Linq.Enumerable.DistinctBy and MoreLinq.MoreEnumerable.DistinctBy

Recently I upgraded one of my projects to use .NET 6 .最近我升级了我的一个项目以使用.NET 6 Earlier I was using MoreLinq library to use DistinctBy() to apply it on the specific property.早些时候,我使用MoreLinq库来使用DistinctBy()将其应用于特定属性。

Now as I upgraded TargetFramework to .NET 6 , this change come up with inbuilt support to DistinctBy() .现在,当我将 TargetFramework 升级到.NET 6时,此更改提供了对DistinctBy()的内置支持。

Now the compiler is confused about which DistinctBy() needs to select, I can't rely on System.Linq , because the removal of using MoreLinq will cause multiple other errors.现在编译器对需要选择哪个DistinctBy()感到困惑,我不能依赖System.Linq ,因为删除using MoreLinq会导致多个其他错误。

I know if we face ambiguity between the same methods then we can use using alias , but I am not sure how to use using alias for Linq extension methods.我知道如果我们在相同方法之间遇到歧义,那么我们可以使用using alias ,但我不确定如何将using alias用于 Linq 扩展方法。

Here is the error:这是错误:

在此处输入图像描述

I am able to replicate the same issue in the below fiddle我可以在下面的小提琴中复制同样的问题

Try online在线尝试

To avoid conflicts when using MoreLinq, while still using them as extension methods, you can import the MoreLinq methods that you need like this:为避免在使用 MoreLinq 时发生冲突,同时仍将它们用作扩展方法,您可以像这样导入所需的 MoreLinq 方法:

using static MoreLinq.Extensions.LagExtension;
using static MoreLinq.Extensions.LeadExtension;

So in your case you could just delete MoreLinq from your usings and then import any MoreLinq extension methods that you need in the file individually as shown above.因此,在您的情况下,您可以从 usings 中删除 MoreLinq,然后在文件中单独导入您需要的任何 MoreLinq 扩展方法,如上所示。

You can read about it on the MoreLinq Github page您可以在MoreLinq Github 页面上了解它

You can't alias an extension method, but you can call the extension like a normal method, using the full namespace.您不能为扩展方法设置别名,但您可以像普通方法一样使用完整的命名空间调用扩展。 After all, extension methods are really just syntactic sugar.毕竟,扩展方法实际上只是语法糖。 For example (using the same data you provided in your fiddle):例如(使用您在小提琴中提供的相同数据):

var inputs =  new []
{
    new {Name = "Bruce wayne", State = "New York"},
    new {Name = "Rajnikant", State = "Tamil Nadu"},
    new {Name = "Robert Downey jr", State = "Pennsylvania"},
    new {Name = "Dwane Johnson", State = "Pennsylvania"},
    new {Name = "Hritik", State = "Maharashtra"}
};
    
var net6DistinctBy = System.Linq.Enumerable.DistinctBy(inputs, x => x.State);
var moreLinqDistinctBy = MoreLinq.MoreEnumerable.DistinctBy(inputs, x => x.State);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM