I have multiple queries that only differ in 1 spot = a boolean check on different entity types. This means that the 'code' is 99% the same. Is it possible to dynamically specify the property that it should check on (func, extension, ...)? This would allow me to turn all those methods in a single method that just accepts the field that it should query on. |
Hello TimM,
Could you please expand the question and add a simplified example so that we could be sure that we understand the idea correctly?
Thank you.
IQueryable<entity> q = ...; var res = q.Where(t => t.BooleanProperty == true);
Question is how I could specify the 't.BooleanProperty == true' part dynamically (as a function argument), so that I don't have to copy/paste my whole query per different single property that I'd like to check on.
I currently have multiple large queries that only differ by a single word.
TimM,
Have you tried construction of the
Where
clause based on indexers? Example:Hope that helps.
Not yet, didn't know that this was possible.
I'll try it out now, thanks!
Can I access properties of structs as well (ex: e["a.b.c"])?
If so, do I need a specific DO version for this kind of support?
AFAIK, such "dot" notation works well for structure and even for reference fields in ordinary scenarios (not inside LINQ queries). However, it might also work for structure fields inside queries (should be tested).
TimM, did it work for you? If so, I could add this idea as an answer and you would tick it as right.
Well, it seems I cannot use the 'dot' (Container["SubContainer.Field"]) but without it (Container.SubContainer["Field"]) it works.
So I wonder if the 'dot' approach works for anyone else, and what version they're using (I don't think we're using the latest version yet).
The 'dot' approach for accessing persistent properties was invented in version 4.4.0, see the announce. As for LINQ translator, seems it supports only accessing properties of the same class by name, like
person => person["Name"]
.