Results 1 to 9 of 9

Thread: [RESOLVED] Passing Query and Field parameters to a Sub

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    Resolved [RESOLVED] Passing Query and Field parameters to a Sub

    Hello, I have a LINQ query:

    VB Code:
    1. Dim Db = New MyDataContext
    2. Dim Qry = From cust in Db.Customers
    3.           Select cust.ID, cust.Name
    And I want to pass it to a function to filter it, like this:

    VB Code:
    1. FilterAnyQuery(Qry, cust.ID) ' WHERE "cust.ID" CAN BE ANY FIELD

    The Sub should be of this kind:
    VB Code:
    1. Sub FilterAnyQuery(ByVal AnyQry As iQueryable, AnyField as ???)
    2.   AnyQry = AnyQry.Where(AnyField = "somevalue")
    3. End Sub

    How can I pass "AnyField" to the Sub and make the LINQ "Where" clause work?
    Is there any workaround?
    Thank you.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    Re: Passing Query and Field parameters to a Sub

    The row:
    vb Code:
    1. FilterAnyQuery(Qry, cust.ID)
    gives the following error:
    "Name 'cust' is not declared".
    Last edited by axplains; Feb 8th, 2010 at 09:04 AM.

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    Re: Passing Query and Field parameters to a Sub

    Well, a "more complete" Sub would be:
    vb Code:
    1. Sub FilterAnyQuery(ByVal AnyQry As iQueryable, AnyField As Object, AValue As String)
    2.   AnyQry = AnyQry.Where(AnyField = AValue)
    3.   DataGridView1.DataSource = AnyQry
    4. 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.

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    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.

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Passing Query and Field parameters to a Sub

    Quote Originally Posted by axplains View Post
    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.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    Re: Passing Query and Field parameters to a Sub

    Thanks a lot Kevin.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width