Delaying LINQ to SQL Select Query Execution

Posted by Maxim Z. on Stack Overflow See other posts from Stack Overflow or by Maxim Z.
Published on 2010-06-14T17:52:37Z Indexed on 2010/06/14 18:12 UTC
Read the original article Hit count: 240

Filed under:
|
|
|
|

I'm building an ASP.NET MVC site that uses LINQ to SQL.

In my search method that has some required and some optional parameters, I want to build a LINQ query while testing for the existence of those optional parameters.


Here's what I'm currently thinking:

using(var db = new DBDataContext())
        {
            IQueryable<Listing> query = null;

            //Handle required parameter
            query = db.Listings.Where(l => l.Lat >= form.bounds.extent1.latitude && l.Lat <= form.bounds.extent2.latitude);

            //Handle optional parameter
            if (numStars != null)
                query = query.Where(l => l.Stars == (int)numStars);

            //Other parameters...

            //Execute query (does this happen here?)
            var result = query.ToList();

            //Process query...

Will this implementation "bundle" the where clauses and then execute the bundled query? If not, how should I implement this feature?

Also, is there anything else that I can improve?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET