tags:

views:

104

answers:

5

I am in the process of writing something that will use Linq to combine results from my database, via Linq2Sql and an in-memory list of objects in order to find out which of my in-memory objects match something on the database.

I've come up with the query in both expression and query syntax.

Expression Syntax

var query = order.Items.Join(productNonCriticalityList,
    i => i.ProductID,
    p => p.ProductID,
    (i, p) => i);

Query Syntax

var query =
    from p in productNonCriticalityList
    join i in order.Items
        on p.ProductID equals i.ProductID
    select i;

I realise that we have all the code completion goodness with expression syntax, and I do actually use that more. Mainly because it's easier to create re-usable chunks of filter code that can be chained together to form more complex filters.

But for a join the latter seems far more readable to me, but maybe that is because I am used to writing T-SQL.

So, am I missing a trick or is it just a matter of getting used to it?

A: 

Well, both statements are equivalent. So you could youse them both, depending on the surrounging code and what is more readable. In my project I make the decision which syntax to use dependent on those two conditions.

Personally I would write the expression syntax in one line, but this is a matter of taste.

Obalix
+1  A: 

It really all comes down to preference. Some people just hate the idea of query like syntax in their code. I for one appreciate the query syntax, it is declarative and quite readable. Like you said though, the chainability of the first example is a nice thing to have. I guess for my money I would keep it query until I felt I needed to begin chaining the call.

Jeremy B.
+1  A: 

I used to feel the same way. Now I find query syntax easier to read and write, particularly when things get complicated. As much as it irked me to type it the first time, 'let' does wonderful things in ways that would not be readable in Expression Syntax.

pdr
@Ruben: Seriously, don't worry about it; I really don't feel that strongly. You're right that it didn't offer much more, I just felt (mildly) that the other duplicate was the one to delete. As I'd had zero upvotes, there's not much to gain from undeleting it now. Let's remove these comments and forget about it - better questions to be answered :)
pdr
A: 

I prefer the Query syntax when its complex and Expression syntax when its a simple query.

If a DBA were to read the C# code to see what SQL we are using, they would understand and digest the Query syntax easier.

Taking a simple example:
Query

var col = from o in orders
          orderby o.Cost ascending
          select o;

Expression

var col2 = orders.OrderBy(o => o.Cost);

To me, the Expression syntax is an easier choice to understand here.


Another example:
Query

var col9 = from o in orders
  orderby o.CustomerID, o.Cost descending
           select o;

Expression

var col6 = orders.OrderBy(o => o.CustomerID).
          ThenByDescending(o => o.Cost);

Both are easy to read and understand, however if the query was

//returns same results as above
var col5 = from o in orders
           orderby o.Cost descending
           orderby o.CustomerID
           select o;
//NOTE the ordering of the orderby's

That looks a little confusing to be as the fields are in a different order and it appears a little backwards.


For Joins
Query

var col = from c in customers
          join o in orders on 
          c.CustomerID equals o.CustomerID
          select new 
          {
              c.CustomerID, 
              c.Name, 
              o.OrderID, 
              o.Cost
          };

Expression:

var col2 = customers.Join(orders, 
    c => c.CustomerID,o => o.CustomerID, 
    (c, o) => new 
        { 
            c.CustomerID, 
            c.Name, 
            o.OrderID, 
            o.Cost 
        }
    );

I find that Query is better.


My summary would be use whatever looks easiest and fastest to understand given the query at hand. There is no golden rule of which to use. However, if there are a lot of joins, I'd go with Query syntax.

Simon Hughes
A: 

I agree with the other responders that the exact question you're asking is simply a matter of preference. Personaly, I mix the two forms depending upon which is clearer for the specific query that I'm writing.

If I have one comment though, I would say that the query looks like it might load all of the items from the order. That might be fine for a single order one time, but if you're looping through lots of orders, it might be more efficient to load all of the items for all of the in one go (you might want to additionally filter by date or customer, or whatever though). If you do that, you might get better results by switching the query around:

var productIds = (from p in productNonCriticalityList
                  orderby p.productID
                  select p.ProductID).Distinct();

var orderItems = from i in dc.OrderItems
                 where productIds.Contains(i.ProductID)
                    && // Additional filtering here.
                 select i;

It's a bit backwards at first glance, but it could save you from loading in all the order items and also from sending lots of queries. It works because the where productIds.Contains(...) call can be converted to where i.ProductID in (1, 2, 3, 4, 5) in SQL. Of course, you'd have to judge it based on the expected number of order items, and the number of product IDs.

Damian Powell
my order.Items are already in memory, but I know what you're saying :)
Antony Scott