Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

.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. 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading