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

The call is ambiguous between the following methods and properties – Linq and MoreLinq

I have a line of code that has been written using MoreLinq here :

var maxPage = _pageState?.Value?.Pages?.MaxBy(p => p.Type.Grids["desktop"].ColCount)?.FirstOrDefault();

Because my solution is using both MoreLinq and Linq I am getting the following error:

The call is ambiguous between the following methods or properties: ‘MoreLinq.MoreEnumerable.DistinctBy<TSource, TKey>(System.Collections.Generic.IEnumerable, System.Func<TSource, TKey>)’ and ‘System.Linq.Enumerable.DistinctBy<TSource, TKey>(System.Collections.Generic.IEnumerable, System.Func<TSource, TKey>)

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 have tried adding the following static extension to my document:
MoreLinq.Extensions.AppendExtension but this errors out FirstOrDefault() at the end of the line with the following error:

‘MyClass’ does not contain a definition for ‘FirstOrDefault’ and no accessible extension method ‘FirstOrDefault’ accepting a first argument of type ‘MyClass’ could be found (are you missing a using directive or an assembly reference?)

I have tried also removing MoreLinq but I get the same does not contain a defition for FirstOrDefault Error.

What is the best way to solve this issue?

>Solution :

MaxBy and DistinctBy methods were introduced in .NET 6 which will result in the aforementioned problem if you are using MoreLinq. If you don’t need any MoreLinq methods just remove it or replace with using System.Linq; (if you are not using the global/implicit usings).

If you still need both you can use the trick with splitting imports on before and after namespace (though it can be a bit esoteric):

// ... common imports
using MoreLinq.Extensions;

namespace YourNameSpace;
{
    using System.Linq; // default LINQ methods will be preferred

    // ... code
}

Or use static imports for only needed methods (see @github):

using static MoreLinq.Extensions.BatchExtension; // import classes holding needed extensions
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