Building dynamic linq query

I am looking to use linq on a forms project I am building for a search page I need. My project is organized into 1 object containing a collection of licenses and a collection of inspections. The license and inspections have a relationship of 1 to many. There is only 1 license per inspection but a license can belong to many inspections. I am building a search page that has many many different items that can be used to search. It can use either all of them or any 1 or combination of the search items to search. The tricky part is the search has to return a collection of inspections although half of the search items are properties in the license object and not the inspection object. Now since it would be totally crazy to build a massive switch statement to do all of this I need a starting point. How do I create a dynamic linq query?  I have not used linq yet but I have read up on using it with objects and I think I get a basic understanding of it.  If anyone could offer me some help I would be grateful.

Thank you in advance,

Scott
LVL 5
scottlafoyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SiliconXPCommented:
hi

i can give it a go if you provide a example of class you are dealing with.(may be class diagram) and also what fields you are searching by. what are are the conditions.
0
scottlafoyAuthor Commented:
Ok, its a little large. Thank you for giving it a try.
Project is the main object containing a list of Inspections and a list of Licenses.

Licenses:
For License I need to search for LicenseNumber

in the WellLicense, the child of License I need: Province, LicenseStatus,  and DownHoleLocation.Uwi.LocationException, DownHoleLocation.Uwi.Lsd, DownHoleLocation.Uwi.Section, DownHoleLocation.Uwi.Township, DownHoleLocation.Uwi.Range, DownHoleLocation.Uwi.Meridian, DownHoleLocation.Uwi.EventSequence, 11 SurfaceLocation.Uwi.LocationException, SurfaceLocation.Uwi.Lsd, SurfaceLocation.Uwi.Section, SurfaceLocation.Uwi.Township, SurfaceLocation.Uwi.Range, SurfaceLocation.Uwi.Meridian, SurfaceLocation.Uwi.EventSequence,

Inspections (the top level object that every type of inspection implements.):
InspectionType, InspectionStatus


The Licenses and Inspections objects are linked through a LicenseKey. Each License has many Inspections but each Inspection has only one license. I need to search for the search items from each inspection, not each license. The search fields are all text based searches.

Thank you for anything you might provide to get me going in the right direction
ClassDiagram.JPG
0
entrygCommented:
If I'm not making mistake, you don't want to load Inspections collection in to License collection by default. Instead you want to load Inspections on demand. If so, you can use LazyList.

http://blog.wekeroad.com/blog/lazy-loading-with-the-lazylist/

Hope this helps.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

scottlafoyAuthor Commented:
I think I do want to load Inspections into License? I want to search for each Inspection so I have to go through each inspection and find its license to get the information I need. All this is new to me so I don't understand it well.
0
scottlafoyAuthor Commented:
so far I think I have come up wth a way to get the license info through the inspection. Am I on the right track here?
var q = from i in myProject.Inspections

                    from l in myProject.Licenses
                     
                    where i.LicenseKey = l.LicenseKey
            
                    select new { i,l };

Open in new window

0
scottlafoyAuthor Commented:
Ok, I got this to work but it returns both License and Inspection, and I need it to only return Inspection so I can cast the results to Inspection and display them any ideas?. I still have no Idea how to dynamicly build querys based on the search items filled in.
var q = from i in myProject.Inspections

                    from l in myProject.Licenses
                     
                    where i.LicenseKey == l.LicenseKey && l.LicenseNumber == textBox1.Text
            
                    select new { i,l };

            var a = q;

Open in new window

0
scottlafoyAuthor Commented:
Forgive my post above, I removed the l from the select new part and it returns just one. That was a little dumb on my part.
0
scottlafoyAuthor Commented:
how could I use an if statement in linq to build a query?
like what I have below.

var q = from i in myProject.Inspections

                    from l in myProject.Licenses

                    where i.LicenseKey == l.LicenseKey && 

                        if(textBox1.Text != string.Empty){i.LicenseNumber == textBox1.Text} &&

                        if(textBox1.Text != string.Empty){i.Location == textBox1.Text}

                    select new { i };

Open in new window

0
Bob LearnedCommented:
I don't believe that LINQ natively supports dynamic queries.  

Scott Guthrie wrote an article that talked about dynamic LINQ queries, that required a library to achieve.

Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
scottlafoyAuthor Commented:
Thank you for your responce.  This answers my question. Unfortunantly I do not have the time to learn and implement IQuerable.  linq has some great potental.        
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.