.net6 & MoreLinq : Call is ambiguous between System.Linq.Enumerable.DistinctBy and MoreLinq.MoreEnumerable.DistinctBy
Asked Answered
S

2

11

Recently I upgraded one of my projects to use .NET 6. Earlier I was using MoreLinq library to use DistinctBy() to apply it on the specific property.

Now as I upgraded TargetFramework to .NET 6, this change come up with inbuilt support to 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.

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.

Here is the error:

enter image description here

I am able to replicate the same issue in the below fiddle

Try online

Steen answered 10/11, 2021 at 17:4 Comment(7)
You can call extension methods just like normal methods if you like, so: MoreLinq.MoreEnumerable.DistinctBy(yourEnumerable, otherParams)Cisterna
PS The reason you can't replicate the problem in your fiddle is that you are missing using System.Linq;Cisterna
This is the beauty of OSS. Just fork MoreLinq and remove its version of DistinctBy.Shank
@DavidG, thanks for your second comment. I fixed it in fiddle and updated the question. Let me try your first commentSteen
@DavidG, your solutions worked for me. You can add it as an answerSteen
@TechInquisitor, I can't do this as I am working on one of the Open source project and this issue I faced while implementing new feature to itSteen
@PrasadTelkikar Do you mean you're contributing to a project that you do not control, so you can't simply change its dependencies? That certainly makes a difference.Shank
C
9

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);
Cisterna answered 10/11, 2021 at 17:46 Comment(0)
P
20

To avoid conflicts when using MoreLinq, while still using them as extension methods, you can import the MoreLinq methods that you need like this:

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.

You can read about it on the MoreLinq Github page

Patriciapatrician answered 17/1, 2022 at 9:0 Comment(0)
C
9

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);
Cisterna answered 10/11, 2021 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.