Link to home
Start Free TrialLog in
Avatar of Kelly Martens
Kelly Martens

asked on

Pass LINQ where clause as Parameter

It really is a simple question yet finding so much trouble with Google finding how to do it. After the where clause I want to return the results from my strongly typed dataset table with a parameter passed from a parameter with vb.net and how to call it. If you can assist I would appreciate it.

Bolded is where I think it should go. But I clearly don't know how to say it. "test" would be something like
"row.Item(columnname) IsNot DBNull.Value AndAlso row.Item(columnname) = filter AndAlso row.TT_Rec_ID = Template_ID" just for example. But the way I see it we should be able to pass anything through that is LINQ compatible.

Function Global_Table_Query([b]ByVal test As Predicate(Of ds2.JOINEDDATADataTable[/b])) As DataTable
        ErrorMessage = ""

        Dim dt As New DataTable
        Dim Is_Good As Byte = 1
               Dim thread As New Task(
  Function()
      Try

          Dim lowNums = From row In Global_Table
                        [b]Where test
[/b]                        Select row


          dt = lowNums.CopyToDataTable
                    Return dt
      Catch ex As Exception
          ErrorMessage = ex.Message
          Return Nothing
      Finally

      End Try
  End Function
            )

        Dim tasks() = {thread}
        Array.ForEach(tasks, Sub(tx) tx.Start())
        Task.WaitAll(tasks)

        For Each tsk In tasks

            Select Case True

                Case tsk.IsCanceled
                    Is_Good = 1

                Case tsk.IsFaulted
                    Is_Good = 1

                Case tsk.IsCompleted
                    Is_Good = 0

                Case Else
                    Is_Good = 1

            End Select
        Next

        tasks = Nothing
        thread = Nothing

       
        'checking if something went wrong
        If ErrorMessage <> "" Then
            Is_Good = Nothing
            Return Nothing
        End If
        Return dt
       
       

    End Function

Open in new window

SOLUTION
Avatar of Fernando Soto
Fernando Soto
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
You could pass the LINQ expression as a parameter of your function, thus you can still reuse the function logic for different LINQ expressions that provide the same result type.
Miguel is correct, you could pass an expression that is generated during run-time; refer to this previous EE PAQ.

That being said, in order for the expression to be generated in the way you want to use it, you will need to collect the meta-data/schema of the database in order to dynamically build your expressions.  If you are dealing with stronly-typed classes which represent your meta-data/schema, this shouldn't pose a problem.

-saige-
Avatar of Kelly Martens
Kelly Martens

ASKER

Miguel and it-sage .... can you show me how you would do the different approaches you have talked about here? Fernando I will check out your idea ....
ASKER CERTIFIED SOLUTION
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 all for your efforts. This is the answer I came up with if your interested....

https://kellyschronicles.wordpress.com/2017/12/16/dynamic-predicate-for-a-linq-query/