DEV Community

vblover programmer
vblover programmer

Posted on

(VB .NET) A Method for Search Values in Several Fields of Entity Framework DataTable

A Method for Search Values in Several Fields of DataTable by UserAccount Filter for Entity Framework

Declaration:

Public DataEntity As New KeyStoreEntities
Function SearchFor(Of T)(value As String, ParamArray Fields() As String) As Objects.ObjectQuery(Of T)
        ListItemButtons.Clear()
        Dim UserAccountParam As New Objects.ObjectParameter("UserAccount", UserAccount.ToLower)
        Dim SearchValueParam As New Objects.ObjectParameter("Value", "%" + value + "%")


        Dim QueryString As String = String.Format("SELECT VALUE {0} FROM {0} WHERE ({0}.UserAccount = @UserAccount)",
                                                  GetType(T).Name)
        For Each Field As String In Fields
            Dim Operate As String = If(Array.IndexOf(Fields, Field) = 0, "AND", "OR")
            QueryString += String.Format(" {0} {1}.{2} LIKE @Value", Operate, GetType(T).Name, Field)
        Next
        Return DataEntity.CreateQuery(Of T)(QueryString, UserAccountParam, SearchValueParam)
    End Function
Enter fullscreen mode Exit fullscreen mode

KeyStoreEntities Is Type of Database (in Entity Framework)

Using:

Dim ListItems As Objects.ObjectQuery(Of Websites) =
                            SearchFor(Of Websites)(UserAccount, Me.ToolStripTextBox1.Text, "Title", "Address", "MailBox",
                                                        "UserName")
Enter fullscreen mode Exit fullscreen mode

Websites is Type of Table (in Entity Framework)

Top comments (0)