Linq Style

M

Michael C

var x = from c in customers where c.FirstName == "John" select c;
vs
var x = customers.Where(c.FirstName == "John");

I've been tossing up for a while which version of linq I prefer and I think
I've settled on the second. It's not quite as friendly to read in some cases
but I think it's clearer what's going on, is usually shorter anyway, you
don't have to switch everything over when you encounter a limitation, you
can use your own functions more easily and it is more powerful.

With regards to it being clearer, sometimes with the first version I find it
gets a little confusing as to what it's actually doing, eg with Multiple
from statements, groups etc it can get a little confusing. If you use
something like "from c in customers from o in c.Orders" then it translates
to a SelectMany which isn't immediately obvious. With the second version
it's always clear what's going on because you can easily see the order from
left to right. I guess I do know the second version better and could gain
more knowledge of the first but I would argue I know the second version
better because it is clearer what's going on.

Being shorter is pretty self explanitory, I haven't done any extensive
keystroke counts but most of what I've translated comes out shorter.

As for not having to switch everything over I find it a pain when you are
half way through writing something and realise you'd like to do something
not supported by the first syntax eg with the second syntax you can get the
index passed into a where clause, eg customers.Where( (c, i) => (i and 1) ==
0) will get every second customer.

With the second syntax your own functions are usable, afaik you can't create
custom keywords to match your custom linq functions. For example, I can
create a LeftJoin function and do something like customers.LeftJoin(orders,
etc) but afaik I can't do "from c in customers leftjoin o in orders
on......"

Being more powerful is fairly obvious, I know you can use a mix to get most
of the functionality but I find the mix kind of ugly and prefer to
consistancy of having the one syntax.

Any opinions?

Cheers,
Michael

PS, sorry about the previous post, I accidentally hit ctrl-enter. Hopefully
it will appear as a subpost of this one.
 
J

Jeff Johnson

PS, sorry about the previous post, I accidentally hit ctrl-enter.
Hopefully it will appear as a subpost of this one.

Not here. Probably because you capitalized the S the second time....

(And I don't think all newsreaders are fooled by that trick, although I know
OE is. It's how I screw over future-posters!)
 
C

Chris Dunaway

var x = from c in customers where c.FirstName == "John" select c;
vs
var x = customers.Where(c.FirstName == "John");

I've been tossing up for a while which version of linq I prefer and I think
I've settled on the second. It's not quite as friendly to read in some cases
but I think it's clearer what's going on, is usually shorter anyway, you
don't have to switch everything over when you encounter a limitation, you
can use your own functions more easily and it is more powerful.

With regards to it being clearer, sometimes with the first version I find it
gets a little confusing as to what it's actually doing, eg with Multiple
from statements, groups etc it can get a little confusing. If you use
something like "from c in customers from o in c.Orders" then it translates
to a SelectMany which isn't immediately obvious. With the second version
it's always clear what's going on because you can easily see the order from
left to right. I guess I do know the second version better and could gain
more knowledge of the first but I would argue I know the second version
better because it is clearer what's going on.

Being shorter is pretty self explanitory, I haven't done any extensive
keystroke counts but most of what I've translated comes out shorter.

As for not having to switch everything over I find it a pain when you are
half way through writing something and realise you'd like to do something
not supported by the first syntax eg with the second syntax you can get the
index passed into a where clause, eg customers.Where( (c, i) => (i and 1) ==
0) will get every second customer.

With the second syntax your own functions are usable, afaik you can't create
custom keywords to match your custom linq functions. For example, I can
create a LeftJoin function and do something like customers.LeftJoin(orders,
etc) but afaik I can't do "from c in customers leftjoin o in orders
on......"

Being more powerful is fairly obvious, I know you can use a mix to get most
of the functionality but I find the mix kind of ugly and prefer to
consistancy of having the one syntax.

Any opinions?

Cheers,
Michael

PS, sorry about the previous post, I accidentally hit ctrl-enter. Hopefully
it will appear as a subpost of this one.

Does the second example even compile? Don't you need a lambda
expression like this:

var x = customers.Where(c => c.FirstName == "John");

As to which is more readable, I suppose it depends on the query. In
the simple example you posted, the direct call to the extension method
seems a bit more readable, but you have to use the lambda
expression. But it gets more complicated when you start throwing in
additional methods like .OrderBy and others. I guess it's a matter
of taste.

Chris
 
A

Arne Vajhøj

Michael said:
var x = from c in customers where c.FirstName == "John" select c;
vs
var x = customers.Where(c.FirstName == "John");

I've been tossing up for a while which version of linq I prefer and I think
I've settled on the second. It's not quite as friendly to read in some cases
but I think it's clearer what's going on, is usually shorter anyway, you
don't have to switch everything over when you encounter a limitation, you
can use your own functions more easily and it is more powerful.

With regards to it being clearer, sometimes with the first version I find it
gets a little confusing as to what it's actually doing, eg with Multiple
from statements, groups etc it can get a little confusing. If you use
something like "from c in customers from o in c.Orders" then it translates
to a SelectMany which isn't immediately obvious. With the second version
it's always clear what's going on because you can easily see the order from
left to right. I guess I do know the second version better and could gain
more knowledge of the first but I would argue I know the second version
better because it is clearer what's going on.

Being shorter is pretty self explanitory, I haven't done any extensive
keystroke counts but most of what I've translated comes out shorter.

As for not having to switch everything over I find it a pain when you are
half way through writing something and realise you'd like to do something
not supported by the first syntax eg with the second syntax you can get the
index passed into a where clause, eg customers.Where( (c, i) => (i and 1) ==
0) will get every second customer.

With the second syntax your own functions are usable, afaik you can't create
custom keywords to match your custom linq functions. For example, I can
create a LeftJoin function and do something like customers.LeftJoin(orders,
etc) but afaik I can't do "from c in customers leftjoin o in orders
on......"

Being more powerful is fairly obvious, I know you can use a mix to get most
of the functionality but I find the mix kind of ugly and prefer to
consistancy of having the one syntax.

I can see a trend. When LINQ first came out the first version
was much more used than the second. Today my impression is
that it has changed. More and more prefer the second version.
Especially the true experts.

We can discuss what is most readable, but there are a big
portion of personal preference in that.

But the above trend is somewhat verifiable.

Arne
 
M

Michael C

Chris Dunaway said:
Does the second example even compile? Don't you need a lambda
expression like this:

var x = customers.Where(c => c.FirstName == "John");

Ah yes, that is a typo. :)
As to which is more readable, I suppose it depends on the query. In
the simple example you posted, the direct call to the extension method
seems a bit more readable, but you have to use the lambda
expression. But it gets more complicated when you start throwing in
additional methods like .OrderBy and others. I guess it's a matter
of taste.

I find the "friendly" method more difficult to work with as the query gets
more complicated. It seems kindof visual basic-ish in that it's designed to
make things simpler but actually makes things more difficult. I guess it is
a matter of taste and I might change my mind in the future.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

linq queries 5
LINQ 101 question 1
linq to sql join 6
Problem in linq. 9
To LinQ or not LinQ 33
LINQ and binding to dataGrid 3
LINQ Newbie 3
are these two linq the same 2

Top