|
-
Feb 8th, 2010, 08:27 AM
#1
Thread Starter
Lively Member
[RESOLVED] Passing Query and Field parameters to a Sub
Hello, I have a LINQ query:
VB Code:
Dim Db = New MyDataContext
Dim Qry = From cust in Db.Customers
Select cust.ID, cust.Name
And I want to pass it to a function to filter it, like this:
VB Code:
FilterAnyQuery(Qry, cust.ID) ' WHERE "cust.ID" CAN BE ANY FIELD
The Sub should be of this kind:
VB Code:
Sub FilterAnyQuery(ByVal AnyQry As iQueryable, AnyField as ???)
AnyQry = AnyQry.Where(AnyField = "somevalue")
End Sub
How can I pass "AnyField" to the Sub and make the LINQ "Where" clause work?
Is there any workaround?
Thank you.
-
Feb 8th, 2010, 08:39 AM
#2
Re: Passing Query and Field parameters to a Sub
Doesn't As Object work? There isn't really any way to narrow the type because you want to pass *ANY* field.
-
Feb 8th, 2010, 08:53 AM
#3
Thread Starter
Lively Member
Re: Passing Query and Field parameters to a Sub
The row:
vb Code:
FilterAnyQuery(Qry, cust.ID)
gives the following error:
"Name 'cust' is not declared".
Last edited by axplains; Feb 8th, 2010 at 09:04 AM.
-
Feb 8th, 2010, 09:26 AM
#4
Re: Passing Query and Field parameters to a Sub
cust is a temporary placeholder, it isn't visible from anywhere except the linq query itself.
you should pass your Qry.
In the procedure itself you won't be able to do Qry.Where
1. In your design such pass is pointless, because you pass your query ByVal so the results will be out of scope after the sub finishes.
2. I really don't see any reason to perform additional Where filtering in a separate sub (it can be done in the first query). Even if there is such a reason you should do another query:
Code:
Sub FilterAnyQuery(ByVal AnyQry As iQueryable)
Dim NewQry = From elements In AnyQry ' Where (your hardcoded condition here)
End Sun
3. Or, you should drop the IQueryable interface and do some stronger typing here (with datasets you can pass the name of the fields or their ordinal index).
-
Feb 8th, 2010, 09:39 AM
#5
Thread Starter
Lively Member
Re: Passing Query and Field parameters to a Sub
Well, a "more complete" Sub would be:
vb Code:
Sub FilterAnyQuery(ByVal AnyQry As iQueryable, AnyField As Object, AValue As String) AnyQry = AnyQry.Where(AnyField = AValue) DataGridView1.DataSource = AnyQry End Sub
so that I can filter the data in a DGV based on value, chosen from a combobox filled with data from a field.
My idea is to have a generic form: with a DGV, pass any LINQ query to the DGV, and filter it on any field.
The complete sub would pass the query, the field and the value to filter the DGV (or maybe, filter a DataBinding behind the DGV).
But maybe my goal is too complicated and I am going too far.
Anyway, thanks for your time.
I can not rate your post: I get a message like "You must spread some reputation around before giving it to cicatrix again"...
Last edited by axplains; Feb 8th, 2010 at 09:44 AM.
-
Feb 8th, 2010, 09:42 AM
#6
Re: Passing Query and Field parameters to a Sub
My guess is you will need to explore Lambda for the where portion of your query, check out the link below.
http://blogs.msdn.com/vbteam/archive...sing-linq.aspx
even thought it's not the direction you want you might consider a hard coded method which works on condition that are taken from data present in a TextBox or a selection from a Combobox etc.
The example which follows queries a DataTable for all data then checks to see if a Textbox has information, if it does the query gets a where condition.
Code:
Dim query = From cust In Table.AsEnumerable _
Select _
CustName = cust.Field(Of String)("CompanyName"), _
ContactName = cust.Field(Of String)("ContactName"), _
Country = cust.Field(Of String)("Country")
If txtCountry.Text.Length > 0 Then
query = query.Where(Function(c) c.Country = txtCountry.Text)
ElseIf txtCompany.Text.Length > 0 Then
query = query.Where(Function(c) c.CustName = txtCompany.Text)
End If
If Not query Is Nothing Then
For Each Customer In query
Console.WriteLine("Name [{0}] Contact [{1}] Country [{2}]", _
Customer.CustName, _
Customer.ContactName, _
Customer.Country)
Next
End If
-
Feb 8th, 2010, 09:48 AM
#7
Thread Starter
Lively Member
Re: Passing Query and Field parameters to a Sub
Thank you for your input Kevin.
As I wrote in my previous post, is there a way to make a more generic routine to filter a LINQ query and feed it to a DataGridView?
I will try to use your method, hopefully will come up with something (still a lot to learn the hard way...)
Thanks a lot.
-
Feb 8th, 2010, 09:53 AM
#8
Re: Passing Query and Field parameters to a Sub
 Originally Posted by axplains
Thank you for your input Kevin.
As I wrote in my previous post, is there a way to make a more generic routine to filter a LINQ query and feed it to a DataGridView?
I will try to use your method, hopefully will come up with something (still a lot to learn the hard way...)
Thanks a lot.
The generic method is listed in the link I provided but is not pretty and requires code that when I tried it (at least 12 months ago) had issues.
-
Feb 8th, 2010, 09:56 AM
#9
Thread Starter
Lively Member
Re: Passing Query and Field parameters to a Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|