This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Perhaps this is a stupid question, but I have to ask. I'm really love QueryExpression, but I missed the nice feature from NHibernate where you can easy make the "criteria/query" dynamic on a easy way. I know thinking about ICriterion (Expression) there you can easy build up a list of ICriterion based on different variables. |
|
|
To some extent this can be done using Entity.Attribute syntax: QueryExpression expr = Entity.Attribute("Fie") == "Zounds"; If you want to vary the operators (e.g. choosing dynamically between && and ||, or between == and !=), then it gets a bit more complex, but it can be done: LogicalOperator op = GetAndOrOr(); RelationalOperator op = GetEqualOrNotEqual(); (and of course you could wrap these in helper methods or a builder library if you were doing a lot of dynamic queries). We'd be happy to look at providing an easier and more unified interface over this stuff -- could you say a bit more about what you want to be able to do and maybe give us an example of your dream API for doing it? Thanks! |
|
|
[quote user="ivan"]QueryExpression expr = Entity.Attribute("Fie") == "Zounds"; Of course :-) [quote user="ivan"]We'd be happy to look at providing an easier and more unified interface over this stuff -- could you say a bit more about what you want to be able to do and maybe give us an example of your dream API for doing it? Thanks! [/quote] After working with QueryExpression I really like it, but the NHibnerate "criterion" is also nice (it easy to read when you have complex dynamic queries). |
|
|
Is possible to make a QueryExpression example library with more complex queries? This is perhaps a stupid question, but what is the best practice to make a (expression OR|AND expression OR|AND expression OR|AND expression) of a List<QueryExpression>
|
|
|
I don't know if there's a specific "best practice"; I'd just use something like: var combinedQuery = exprs.Aggregate((q1, q2) => q1 && q2); If you need to be able to choose OR and AND differently at each position then it depends how that additional information is represented, and you also have to start worrying about precedence because (q1 AND q2) OR q3 is not the same as q1 AND (q2 OR q3). |
|
|
As I said so could this no be a big problem, but lets say that I have a List<queries> that I need to be (q OR q OR q OR q) etc. Currently i'm not getting the effect I want... [quote user="ivan"]If you need to be able to choose OR and AND differently at each position then it depends how that additional information is represented, and you also have to start worrying about precedence because (q1 AND q2) OR q3 is not the same as q1 AND (q2 OR q3).[/quote] of course, thats why i'm trying to figure out this. |
|
|
Hello Christian, We don't have any specific recommendations on this. Effectively what you are talking about is building and traversing a tree, where each leaf node is labelled with a query expression and each non-leaf node is labelled AND or OR. In terms of traversing the tree, this should be a routine matter of recursion, using Children.Aggregate((q1, q2) => q1 && q2) for AND nodes and Children.Aggregate((q1, q2) => q1 || q2) for OR nodes (leaf nodes of course just returning the QueryExpression with which they are labelled). In terms of defining and building the tree structure, I'd suggest having a look at a data structures book, though in reality any manageable query is going to have such a simple tree that there's no point going for anything fancy -- a base node class with a List<Node> Children property and a GetQueryExpression method, and derived classes for AND, OR and QueryExpression nodes with the overrides mentioned above. Of course, if there's something specific in the way that QueryExpressions are working for you that isn't "getting the effect you want" then let us know! |
|
|
This is not use in a tree...it is a list of Between's. I'm going to try some other ways to get my what I want/need.. [quote user="ivan"]I'd suggest having a look at a data structures book, though in reality any manageable query is going to have such a simple tree that there's no point going for anything fancy [/quote] You should see my bookshelf at home and which books I have on my night table before you suggesting this ;P |
|
|
For Betweens, can't you just aggregate them with the || operator? ranges.Select(r => Entity.Attribute(rangeColumn).Between(r.Low, r.High)).Aggregate((q1, q2) => q1 || q2); or something like that...? (not tested!) |
|
|
Yeah think so, i'm not 100% sure how to use all the functions in Aggregate Your example generate ((BETWEEN or BETWEEN) OR (BETWEEN)), not (BETWEEN or BETWEEN or BETWEEN) |
|
|
LightSpeed logical expressions are always binary. There's no way to force LightSpeed to generate (a OR b OR c); it will always generate ((a OR b) OR c) or (a OR (b OR c)). But (unless my rusty mathematical logic is letting me down) these three expressions are exactly equivalent, so I don't think there's any need to worry about the exact form of the generated SQL. Feel free to mock me if I've overlooked an obvious difference *grin*. |
|
|
I probably should look at another way to do this then :-) I hope the forum support the quote "No question is too dumb. Only lots of dumb answers." |
|