|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Linq Stylevs 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. "Michael C" <mike@nospam.com> wrote in message Not here. Probably because you capitalized the S the second time....news:%23HWdHK3XJHA.5476@TK2MSFTNGP03.phx.gbl... > PS, sorry about the previous post, I accidentally hit ctrl-enter. > Hopefully it will appear as a subpost of this one. (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!)
Show quote
Hide quote
On Dec 16, 5:36 am, "Michael C" <m...@nospam.com> wrote: Does the second example even compile? Don't you need a lambda> 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. 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 "Chris Dunaway" <dunaw***@gmail.com> wrote in message news:070c5e29-96a1-4570-912f-> Does the second example even compile? Don't you need a lambda Ah yes, that is a typo. :-)> 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 I find the "friendly" method more difficult to work with as the query gets > 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. 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 Michael C wrote:
Show quoteHide quote > var x = from c in customers where c.FirstName == "John" select c; I can see a trend. When LINQ first came out the first version> 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. 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
Other interesting topics
get object size C#
Truely unique file name. Getting the oldest file in a directory. Perplexed on logging both text and binary information to a file How to reference in c# script? Am i calling the database twice? SQLException - message property was empty Static methods/properties in interface? get namespace & classes in other projects unable to handle system.dll |
|||||||||||||||||||||||