0
1

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.

asked Dec 07 '11 at 10:12

TimM's gravatar image

TimM
9779

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.

(Dec 07 '11 at 12:37) Dmitri Maximov Dmitri%20Maximov's gravatar image

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.

(Dec 08 '11 at 03:08) TimM TimM's gravatar image

TimM,

Have you tried construction of the Where clause based on indexers? Example:

string propertyName;
object value;

var result = q.Where(e => e[propertyName] == propertyValue);

Hope that helps.

(Dec 08 '11 at 03:13) Dmitri Maximov Dmitri%20Maximov's gravatar image

Not yet, didn't know that this was possible.

I'll try it out now, thanks!

(Dec 08 '11 at 03:15) TimM TimM's gravatar image

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?

(Dec 08 '11 at 08:02) TimM TimM's gravatar image

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

(Dec 08 '11 at 08:17) Dmitri Maximov Dmitri%20Maximov's gravatar image

TimM, did it work for you? If so, I could add this idea as an answer and you would tick it as right.

(Dec 14 '11 at 06:23) Dmitri Maximov Dmitri%20Maximov's gravatar image

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

(Dec 14 '11 at 07:17) TimM TimM's gravatar image

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

(Dec 14 '11 at 07:31) Dmitri Maximov Dmitri%20Maximov's gravatar image

One Answer:

also you can generate expression dynamically: link

answered Dec 08 '11 at 04:16

proff's gravatar image

proff
75236

Are you sure that it works with dataobjects?

(Dec 08 '11 at 07:57) TimM TimM's gravatar image

Yes, but not used it with dataobjects.

(Dec 08 '11 at 08:10) proff proff's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×57
×17
×6

Asked: Dec 07 '11 at 10:12

Seen: 2,054 times

Last updated: Dec 14 '11 at 07:33

powered by OSQA