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.
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.