Link to home
Start Free TrialLog in
Avatar of stevejung
stevejung

asked on

query ienumerable results

I have the linq query below which returns a bunch of results. Now I want to narrow down that query more. There are certain columns the user can choose to group by and I need to narrow it down until I have looped through all the columns that the user chose to group by.

I am not real sure how to explain it but basically with qry that gets returned below I need to be able to  do this:

qry=qry.select("A column from one of the tables" = "A specified value")

the number of times and names of the columns that it narrows by can change all the time.
Dim qry = From m In dsMaterial.tbMaterial Join s In dsMaterial.tbMatInventory On m.MaterialKey Equals s.MaterialKey

Open in new window

Avatar of Salim Fayad
Salim Fayad
Flag of Lebanon image

This is what i suggest. Let's suppose you have 3 possible columns that the user can filter by one, none, 2 or all of them. They are Col1, Col2 and Col3 in table dsMaterial.tbMaterial. And you have 3 textboxes TxtCol1, TxtCol2 and TxtCol3 for the user to do a search with whatever he wants. Your query will be as following:

Dim qry = From m In dsMaterial.tbMaterial Join s In dsMaterial.tbMatInventory On m.MaterialKey Equals s.MaterialKey
Where (TxtCol1.Text = "" Or TxtCol1.Text = m.Col1)
    AND (TxtCol2.Text = "" Or TxtCol2.Text = m.Col2)
    AND (TxtCol3.Text = "" Or TxtCol3.Text = m.Col3)

Open in new window

This may be overkill, but you may consider utilizing one of the "dynamic query" methods that are out there, such as the work of Tomas Petricek (http://tomasp.net/blog/dynamic-linq-queries.aspx - see especially section "Dynamic Queries #2")  or a similar (I believe more recent) approach (based on his original code) in LinqKit (http://www.albahari.com/nutshell/linqkit.aspx) which also includes PredicateBuilder - a LINQ extension that allows for the dynamic creation of predicates. Either of these might help in the development of a much more generalized solution - such as one that would allow for richer user interaction with respect to building the dynamic query.

Both of these approaches utilize the approach of building and evaluating Expressions on the fly, depending on user specified criteria.

PredicateBuilder will be really valuable if you decide to let the user decide whether his/her selected and submitted multi-valued criteria ought to be ANDed or ORed.

IMO, this approach involves some really cool stuff...and worth the effort of wrapping your head around. If it doesn't help solve a problem today, it likely will in the future.

I hope this helps.
Avatar of stevejung
stevejung

ASKER

The first comment won't work because I don't know the names of the columns because they are based on the way a grid is grouped. I will look closer at the second but what I really wanted to be able to do was run a query against the results of another query. Is there any way to do that? In my example above, once the qry is returned I would like to run a query against the qry results that were already returned.
ASKER CERTIFIED SOLUTION
Avatar of novynov
novynov
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you I did not fully understand how the linq queries worked I did not realize that they are not executed right away.