0

I'm trying to do OrderBy, but receiving the following error:

The call is ambiguous between the following methods or properties: 'System.Linq.Dynamic.DynamicQueryable.OrderBy(System.Linq.IQueryable, string, params object[])' and 'System.Linq.Dynamic.DynamicQueryable.OrderBy(System.Linq.IQueryable, string, params object[])'

 int start = Convert.ToInt32(Request["start"]);
            int length = Convert.ToInt32(Request["length"]);
            string searchValue = Request["search[value]"];
            string sortColumnName = Request["columns[" + Request["order[0][column]"] + "][name]"];
            string sortDirection = Request["order[0][dir]"];

            List<Account> tAccounts = new List<Account>();

            using (Entities dbcontext = new Entities())
            {
                tAccounts = dbcontext.Accounts
                    .AsQueryable()
                    .OrderBy(sortColumnName + " " + sortDirection)
                    .Skip(start)
                    .Take(length)
                    .ToList();

I have changed the code to the following as System.Linq.Dynamic was causing ambiguous between the different methods, but now have a new issue IQueryable does not contain a definition for 'ToList' any pointers much appreciated.

 var tAcc = from x in dbcontext.Accounts
                                select x.AsEnumerableOfOne()
                                .OrderBy(sortColumnName + " " + sortDirection)
                                .Skip(start)
                                .Take(length)
                                .ToList();

                tAccounts = tAcc.ToList<Account>();
Joe Black
  • 81
  • 1
  • 8
  • Does this answer your question? [Dynamic LINQ OrderBy on IEnumerable / IQueryable](https://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet-iqueryablet) – Klaus Gütter Jul 19 '22 at 18:14
  • Which dynamic LINQ library do you use? There are several forks. Or, since the method signatures are equal, do you maybe reference two libraries? – Gert Arnold Jul 19 '22 at 18:23
  • using System.Linq.Dynamic; using System.Linq; – Joe Black Jul 19 '22 at 18:27
  • That doesn't answer my question. Look at the project dependencies/NuGet packages. – Gert Arnold Jul 19 '22 at 18:29
  • @GertArnold I have System.Linq.Dynamic v1.0.8 and System.LinqExpressions v4.3.0 installed – Joe Black Jul 19 '22 at 18:45
  • The error message means you somehow have two definitions for the same method - in your development environment, can you search for the duplicate definitions? – NetMage Jul 19 '22 at 19:03

1 Answers1

0

Firstly, you can remove the AsQueryable(), this is only required if you want an IQueryable() reference for a DbSet.. I.e. var query = dbContext.Accounts.AsQueryable(); if (someCondition) query = query.Where(/*condition*/); When building a Linq Query using operations that result in IQueryable the AsQueryable() is not required.

That said, the error you have is that the same Extension method signature is defined in multiple referenced assemblies, annoyingly using the exact same namespace. (That is a bit of a no-no) That it is an extension method is a pain in the butt as these are global static methods.

The culprit is most likely System.Linq.Dynamic. What are you using from this library because this library has been marked as Deprecated by Microsoft. Chances are it was a stop-gap during .Net Core development until all of its capabilities were integrated in their new homes in order Linq related assemblies. I would suggest removing this package, then investigating each breaking method (if used) and searching/listing specific breaking cases to determine how best to migrate them.

Steve Py
  • 26,149
  • 3
  • 25
  • 43