From: Bob on 25 May 2010 00:58 Say there are 3 fields in an object and you'd like to dynamically create a query that my, or may not, filter on any combination of fields... How woud you do that without using expression trees? Here's the class: public class coloredObject: { string size; striing color; string weight; } The end user can specify one, two, or all three fields... Query 1: .... where coloredObj.size > 12 Query 2: .... where coloredObj.color == red Query 3: .... where coloredObj.size > 12 && coloredObj.color == red Is there any simple way to accomplish that 'on the fly' in generalized form rather than having to test conditionals in front of a stack of individual specialialized queries? I've seen the 'stack of conditional-query-condit-query' used in a book on LINQ, so I'm not getting my hopes up. But I don't want to resort to Expression Trees either.
From: Thes on 25 May 2010 04:45 On 25 May, 05:58, Bob <b...(a)nope.com> wrote: > Say there are 3 fields in an object and you'd like to dynamically > create a query that my, or may not, filter on any combination of > fields... How woud you do that without using expression trees? > > Here's the class: > > public class coloredObject: > { > string size; > striing color; > string weight; > } > > The end user can specify one, two, or all three fields... > > Query 1: > ... where coloredObj.size > 12 > > Query 2: > ... where coloredObj.color == red > > Query 3: > ... where coloredObj.size > 12 && coloredObj.color == red > > Is there any simple way to accomplish that 'on the fly' in generalized > form rather than having to test conditionals in front of a stack of > individual specialialized queries? > > I've seen the 'stack of conditional-query-condit-query' used in a > book on LINQ, so I'm not getting my hopes up. But I don't want to > resort to Expression Trees either. You can take advantage of the fact that your query is not actually carried out until until you use the results. So you can actually ocnditionally modify your query without any cost. So you could do something like (untested): List<colouredObject> colouredObjectList = ... .... var results = from c in colourObjectList where <some primary condition> select c; if(<some secondary condition>) { results = results.Where(c => c.size > 12); } if(<some other secondary condition>) { results = results.Where(c => c.colour == red); } // Use the results foreach(colouredObject result in results) { ... } I know its probably only halfway to what you're after, but its easy enough and avoid fiddling about directly with expression trees. Thes.
From: Bob on 26 May 2010 04:49 On Tue, 25 May 2010 01:45:48 -0700 (PDT), Thes <thesmusic(a)myway.com> wrote: >On 25 May, 05:58, Bob <b...(a)nope.com> wrote: >> Say there are 3 fields in an object and you'd like to dynamically >> create a query that my, or may not, filter on any combination of >> fields... How woud you do that without using expression trees? >> >> Here's the class: >> >> public class coloredObject: >> { >> � �string size; >> � �striing color; >> � �string weight; >> �} >> >> The end user can specify one, two, or all three fields... >> >> Query 1: >> ... � �where coloredObj.size > 12 >> >> Query 2: >> ... � where coloredObj.color == red >> >> Query 3: >> ... �where coloredObj.size > 12 && coloredObj.color == red >> >> Is there any simple way to accomplish that 'on the fly' in generalized >> form rather than having to test conditionals in front of a stack of >> individual specialialized queries? >> >> I've seen the 'stack of �conditional-query-condit-query' used in a >> book on LINQ, so I'm not getting my hopes up. But I don't want to >> resort to Expression Trees either. > >You can take advantage of the fact that your query is not actually >carried out until until you use the results. So you can actually >ocnditionally modify your query without any cost. > >So you could do something like (untested): > >List<colouredObject> colouredObjectList = ... >... >var results = from c in colourObjectList > where <some primary condition> > select c; > >if(<some secondary condition>) >{ > results = results.Where(c => c.size > 12); >} > >if(<some other secondary condition>) >{ > results = results.Where(c => c.colour == red); >} > >// Use the results >foreach(colouredObject result in results) >{ > ... >} > >I know its probably only halfway to what you're after, but its easy >enough and avoid fiddling about directly with expression trees. > >Thes. Hi Thes,
From: Bob on 26 May 2010 04:54 On Tue, 25 May 2010 01:45:48 -0700 (PDT), Thes <thesmusic(a)myway.com> wrote: >On 25 May, 05:58, Bob <b...(a)nope.com> wrote: >> Say there are 3 fields in an object and you'd like to dynamically >> create a query that my, or may not, filter on any combination of >> fields... How woud you do that without using expression trees? >> >> Here's the class: >> >> public class coloredObject: >> { >> � �string size; >> � �striing color; >> � �string weight; >> �} >> >> The end user can specify one, two, or all three fields... >> >> Query 1: >> ... � �where coloredObj.size > 12 >> >> Query 2: >> ... � where coloredObj.color == red >> >> Query 3: >> ... �where coloredObj.size > 12 && coloredObj.color == red >> >> Is there any simple way to accomplish that 'on the fly' in generalized >> form rather than having to test conditionals in front of a stack of >> individual specialialized queries? >> >> I've seen the 'stack of �conditional-query-condit-query' used in a >> book on LINQ, so I'm not getting my hopes up. But I don't want to >> resort to Expression Trees either. > >You can take advantage of the fact that your query is not actually >carried out until until you use the results. So you can actually >ocnditionally modify your query without any cost. > >So you could do something like (untested): > >List<colouredObject> colouredObjectList = ... >... >var results = from c in colourObjectList > where <some primary condition> > select c; > >if(<some secondary condition>) >{ > results = results.Where(c => c.size > 12); >} > >if(<some other secondary condition>) >{ > results = results.Where(c => c.colour == red); >} > >// Use the results >foreach(colouredObject result in results) >{ > ... >} > >I know its probably only halfway to what you're after, but its easy >enough and avoid fiddling about directly with expression trees. > >Thes. Hi Thes, That's what I was referring to above as 'stack of conditional-query-condit-query'. I was thinking primarily about the efficiency of the query, but your mention of deferred execution brings up a good point: Does that allow optimization of the split-up queries? It seems likely that a SQL query could be optimized across the multiple statements but I'd also like to make sure XML or collection-based queries don't bog down. Do you have any info on the question of optimization?
From: Thes on 26 May 2010 06:20
On 26 May, 09:54, Bob <b...(a)nope.com> wrote: > On Tue, 25 May 2010 01:45:48 -0700 (PDT), Thes <thesmu...(a)myway.com> > wrote: > > > > > > >On 25 May, 05:58, Bob <b...(a)nope.com> wrote: > >> Say there are 3 fields in an object and you'd like to dynamically > >> create a query that my, or may not, filter on any combination of > >> fields... How woud you do that without using expression trees? > > >> Here's the class: > > >> public class coloredObject: > >> { > >> string size; > >> striing color; > >> string weight; > >> } > > >> The end user can specify one, two, or all three fields... > > >> Query 1: > >> ... where coloredObj.size > 12 > > >> Query 2: > >> ... where coloredObj.color == red > > >> Query 3: > >> ... where coloredObj.size > 12 && coloredObj.color == red > > >> Is there any simple way to accomplish that 'on the fly' in generalized > >> form rather than having to test conditionals in front of a stack of > >> individual specialialized queries? > > >> I've seen the 'stack of conditional-query-condit-query' used in a > >> book on LINQ, so I'm not getting my hopes up. But I don't want to > >> resort to Expression Trees either. > > >You can take advantage of the fact that your query is not actually > >carried out until until you use the results. So you can actually > >ocnditionally modify your query without any cost. > > >So you could do something like (untested): > > >List<colouredObject> colouredObjectList = ... > >... > >var results = from c in colourObjectList > > where <some primary condition> > > select c; > > >if(<some secondary condition>) > >{ > > results = results.Where(c => c.size > 12); > >} > > >if(<some other secondary condition>) > >{ > > results = results.Where(c => c.colour == red); > >} > > >// Use the results > >foreach(colouredObject result in results) > >{ > > ... > >} > > >I know its probably only halfway to what you're after, but its easy > >enough and avoid fiddling about directly with expression trees. > > >Thes. > > Hi Thes, > > That's what I was referring to above as 'stack of > conditional-query-condit-query'. Ooh - I see! I should RTFMessage properly! > I was thinking primarily about the > efficiency of the query, but your mention of deferred execution brings > up a good point: Does that allow optimization of the split-up queries? > It seems likely that a SQL query could be optimized across the > multiple statements but I'd also like to make sure XML or > collection-based queries don't bog down. Do you have any info on the > question of optimization?- Hide quoted text - I don't, I'm afraid. Nothing short of Googling. Perhaps someone with more expertise in such can wade in... Thes. |