IQueryable does not contain a definition for 'ToList'

Joe

Member
Joined
Jun 22, 2022
Messages
7
Programming Experience
3-5
Hi, I'm trying to do the following, but receiving this error:

'IQueryable<List<Account>>' does not contain a definition for 'ToList' and the best extension method overload 'Enumerable.ToList<Account>(IEnumerable<Account>)' requires a receiver of type 'IEnumerable<Account>'

Any pointers much appreciated.

Code:
);
             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>();

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

           tAccounts = tAcc.ToList<Account>();
 
Last edited:
Is the error on line 14?
 
Anyway, why are you even using that AEnumerableOfOne() ? I hope that you realize that the lines 9-14 ends up being parsed by the compiler as:
C#:
var tAcc = from x
           in dbcontext.Accounts
           select (x.AsEnumerableOfOne()
                    .OrderBy(sortColumnName + " " + sortDirection)
                    .Skip(start)
                    .Take(length)
                    .ToList());

And hence you end up with a IQueryable<List<Account>> in tAcc.

So assuming that your [icode[OrderBy(some magic string)[/icode] even works, what you really want is something like this instead:
C#:
var tAccounts = dbcontext.Accounts
                         .OrderBy(sortColumnName + " " + sortDirection)
                         .Skip(start)
                         .Take(length)
                         .ToList();

You don't need line 7 or 16 anymore.
 
Thanks Skydiver,

With your example I'm receiving another error at OrderBy;

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

I'm using
using System.Linq;
using System.Linq.Dynamic;

and when I commit out System.Linq.Dynamic, I receive another error;

'DbSet<Account>' does not contain a definition for 'OrderBy' and the best extension method overload 'PublishedContentExtensions.OrderBy(IEnumerable<IPublishedContent>, string)' requires a receiver of type 'IEnumerable<IPublishedContent>'

Sometimes i wished i worked as a plumber : )
 
Yes, that because the real out of the box OrderBy() require a lambda that returns the key. That's why in my post #3, I specifically said "So assuming that your OrderBy(some magic string) even works...".

Here's the real out of the box OrderBy():
 
Back
Top Bottom