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)

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up