Avatar of Evan Cutler
Evan Cutler
Flag for United States of America asked on

IQueryable expression output

I have this code:
start = 0;
        rp = 5;
        sortname = "Title";
        sortorder = "OrderBy";

        IQueryable<Advertisement> ads = (from ad in ct.Advertisements
                                        select ad).Skip(start).Take(rp);

        total = ads.Count();

        ParameterExpression AdParam = Expression.Parameter(typeof(Advertisement), "ad");
        MemberExpression Admbr = Expression.PropertyOrField(AdParam, sortname);
        LambdaExpression adLambda = Expression.Lambda(Admbr, AdParam);

        Type[] exprArgTypes = { ads.ElementType, adLambda.Body.Type };

        MethodCallExpression AdCall = Expression.Call(typeof(Queryable), sortorder, exprArgTypes, ads.Expression, adLambda);

        IQueryable orderedads = ads.Provider.CreateQuery(AdCall);

Open in new window

The code creates a really pretty SQL to be fired.

I need to fire the SQL so I can get a List<Advertisements> filled up, or to have ads requery so I can invoke foreach, and get my data out.

Right now, I have the IQueryable expression output.  what's next?
.NET ProgrammingC#

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon

orderedads.ToList() will execute your query and return List<Advertisement>. The result can then be iterated with foreach.

List<Advertisement> adList = orderedads.ToList();
foreach(Advertisement ad in adList)
Evan Cutler

it gives me this error...
Am I missing something?

'System.Linq.IQueryable' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?)      
Fernando Soto

Hi arcee123;

The IQueryable interface type doen't have a method ToList to use. Convert the IQueryable to a Enumerable and then apply the ToList  method, so try it like this:

List<Advertisement> adList = orderedads.AsEnumerable().ToList();
foreach(Advertisement ad in adList)


Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Evan Cutler

gives me two errors now:

'System.Linq.IQueryable' does not contain a definition for 'AsEnumerable' and the best extension method overload 'System.Data.DataTableExtensions.AsEnumerable(System.Data.DataTable)' has some invalid arguments      c:\Users\Evan\Documents\Visual Studio 2010\WebSites\ResourceD\Scratchpad\Ads.ashx


Instance argument: cannot convert from 'System.Linq.IQueryable' to 'System.Data.DataTable'      c:\Users\Evan\Documents\Visual Studio 2010\WebSites\ResourceD\Scratchpad\Ads.ashx

This is hard...lol
Fernando Soto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Evan Cutler

That worked.... :)

Thanks much.
Fernando Soto

Not a problem, glad I was able to help.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.