views:

56

answers:

2

I have a survey that's stored in a large object graph of variable depth, depending on how many sections and sub-sections the user chooses to create. I need to be able to search through all the properties for each object in the object graph and see if that property's .ToString() contains a certain keyword that's being searched for.

Can I use LINQ to accomplish this, or do I have to use reflection and loops? The goal is to be able to say something like "Find me all objects in this object graph where one (or more) of its properties contains the substring test".

+3  A: 

LINQ is not the right tool for this - this should be impossible with the build-in query operations without heavy usage of reflection in the queries.

You can of course traverse the object graph using reflection but this is not going to be a fast solution and you may have to account for some nasty things like cycles in the object graph.

If the classes constituting the object graph are under your control I strongly suggest to build this functionality into the classes. You could for example create an interface and implement on all classes. Then you can recursively analyze the object graph without relying on reflection.

Daniel Brückner
Thanks, that's what I was thinking about doing but I wanted to make sure this wasn't possible in LINQ first.
Daniel T.
A: 

If I understand correctly, you are trying to use LINQ to flatten out a tree structure. For this I used Generate from MoreLinq. But I had to create my own Generate in the case an object can have more than 1 child. Reply in the comments if you'd like to see my implementation and sample code.

Yuriy Faktorovich