How to use StartsWith function in Linq to Sql

By bryan tugade

This topic will show us on how to find data that starts with a given character using StartsWith function in Linq to Sql.

Imports System.Linq

Partial Class _Default
    Inherits System.Web.UI.Page

Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
       Dim pContext = New mydataclassesDataContext()

         Dim pr = (From p In pContext.Products Where p.Name.StartsWith("A") Select p Take 10).ToList


        For Each prodx In pr
           Response.Write(prodx.Name & "<br/>")
        Next

    End Sub
End Class

Note : make sure that the given parameter for character or string in StartsWith has a correct casing. If the data starts with Uppercase the parameter should be uppercase.
I used AdventureWorks as my database for this topic.

Related FAQs

We can select multiple fields in a given database tables using Linq to sql. I use AdventureWorks as my database in this sample app and to show you how it works i bind the results in gridview control.
We can easily bind gridview using linq to sql. Assuming that your using AdventureWorks as your database.
We could get specific rows in the database table by skip and take using Linq to Sql. Here is the sample code.
We can show Distinct data in gridview using Linq to SQL. We can compare in the result how the distinct function works in Linq to sql. Here's how.
How to use StartsWith function in Linq to Sql  (1389 Views)