LINQ to SQL - Dynamically Constructing Queries - Um... No!

Mike Taulty is talking about constructing queries dynamically in Linq to SQL. Sorry, but my response to that is Yuck! Here is the code that he shows:

  NorthwindDataContext ctx = new NorthwindDataContext("server=.;database=northwind");

   var query = from c in ctx.Customers
               where c.Country == "Germany"
               select c;

   if (RuntimeCriterionOneApplies()) 
   {
    query = from c in query
            where c.City == "Berlin"
            select c;
   }

   if (RuntimeCriterionTwoApplies())
   {
    query = from c in query
            where c.Orders.Sum(o => o.Freight) > 100
            select c;
   }

   foreach (Customer c in query)
   {
    Console.WriteLine(c.CustomerID);
   }

This is not constructing queries dynamically, it is choosing a query dynamically. This isn't what I talk about when I am talking about constructing a query dynamically. You can look here as what I consider a complex query that is being constructed dynamically.

Mike, how would you handle that scenario in Linq?

Update:  I really should pay more attention when I am reading the code. Sorry Mike, the code that you have will work for the scenario that I have. I don't think that the syntax is very elegant for this scenario, but it will work. I missed the "from q in query" and read is as if a new query were constructed in each if.

Print | posted on Tuesday, April 24, 2007 12:48 PM

Feedback


# re: LINQ to SQL - Dynamically Constructing Queries - Um... No! 4/24/2007 3:03 PM Mike

Hi,

Yep, agree that I'm largely _assembling_ pre-canned bits of queries in my post which is what someone specifically asked me about.

Having said that, I had a look at the insurance form example and it looks like it does;

1) Adds where clauses for Equals, LIKE, Exists, LE, GE.
2) Adds joins to other tables.

I think (1) and (2) can be worked out from what I posted so
there's clearly a crucial part of what you're doing that's dynamic in a way that wasn't - can you elaborate a bit?

Thanks,

Mike.


Gravatar

# re: LINQ to SQL - Dynamically Constructing Queries - Um... No! 4/24/2007 3:43 PM Ayende Rahien

Mike,
The idea here that I have a need to assemble a query based on user input, so I look at the input and add the condition if it exists.

The idea is that I would:
A/ Have an _easy_ and _maintainable_ way to build the query, and add / remove condition when the UI / requirements change
B/ Would generate a query for just the condition that I want.

> a crucial part of what you're doing that's dynamic in a way that wasn't

I don't understand your meaning here.

The idea is to filter only by the fields that the user has specified, so it has to be dynamic.


Gravatar

# re: LINQ to SQL - Dynamically Constructing Queries - Um... No! 4/24/2007 4:01 PM Mike

Ok, so in my original example I'm struggling to see the difference between where you have;

if (user populated field X)
// add some predicate

and I have;

if ( RuntimeCreterionOneApplies() )
// add some predicate

in that in both cases we seem to dynamically add predicates into the where clauses and/or add tables to join to?

So...I was looking for which bit it is where you want me to be "more dynamic" :-)

Mike.


Gravatar

# re: LINQ to SQL - Dynamically Constructing Queries - Um... No! 4/24/2007 4:33 PM Ayende Rahien

Mike, sorry, you are correct. I didn't read the code carefully enough.
I updated the post accordingly.


Gravatar

# re: LINQ to SQL - Dynamically Constructing Queries - Um... No! 4/24/2007 5:18 PM Luke Breuer

Both of you *do* realize that LINQ is simply a bunch of function calls that return IEnumerable and therefore can be built dynamically by "adding" function calls instead of the LINQ syntax sugar, right?

Now, you might want to take into account that I probably have a very distorted view of query building, as the main product my company sells is a GUI that lets the customer generate just about any projection possible with SQL. :-)


# re: LINQ to SQL - Dynamically Constructing Queries - Um... No! 4/24/2007 5:59 PM Mike

Luke,

Yes. I wrote some stuff about that here;

http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/01/31/9085.aspx

Thanks,

Mike.


# re: LINQ to SQL - Dynamically Constructing Queries - Um... No! 4/24/2007 6:01 PM Mike

Actually - just to edit my own comment :-)

It's not _always_ about IEnumerable and it's really not that much about IEnumerable when we come to Linq to SQL. It's more about IQueryable at that point.

Mike.


Gravatar

# re: LINQ to SQL - Dynamically Constructing Queries - Um... No! 4/24/2007 7:30 PM Luke Breuer

Mike - this is true; all we really care about is deferred execution. If we're doing in-memory LINQ, then there are some performance issues to worry about (putting where clauses before joins when possible), but not for DLINQ (err, LINQ to SQL).

Comments have been closed on this topic.