LIKE query on DataTable using LINQ

Mad Dog Tannen

Ive just started learning Linq. Ive just created a sample test like this.

            Dim dt As New DataTable

            Dim dc As New DataColumn

            dc.ColumnName = "Test"
            dt.Columns.Add(dc)

            dt.Rows.Add("Test")
            dt.Rows.Add("One test")
            dt.Rows.Add("Second test")

            Dim results = From myRow In dt.AsEnumerable
                          Where myRow("Test") = "Test"
                          Select myRow


            For Each Row In results
                Console.WriteLine(Row.Item(0).ToString())
            Next

This returns the first row in the iteration.

But what if i want to use a LIKE operator using %? I cant get it to work.

Ive tried

Where myRow("Test") LIKE "Test%"
James

Sounds to me like you want to use StartsWith i.e.

 Dim results = From myRow In dt.AsEnumerable
               Where myRow("Test").StartsWith("Test")
               Select myRow

Contains will match anywhere in the string where as StartsWith will only match if it's at the beginning of the string (same logic as Test%).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Group by on datatable by using linq query

From Dev

LINQ to DataTable Join Query using OR condition

From Dev

Update like Sub query using Linq to Sql

From Dev

C# MVC - Using LIKE in LINQ query

From Dev

Using LINQ Query DataTable using partial column names

From Java

LINQ query on a DataTable

From Dev

Linq grouping query to DataTable

From Dev

How to group DataTable results using Linq query expression

From Dev

Linq Query on DataTable and Update Records

From Dev

How to run Linq on DataTable with SQL like 'IN' and return DataTable C#?

From Dev

Search datatable using Linq to dataset

From Dev

Using Linq to GroupBy and Sum datatable

From Dev

Using LINQ and Storing Results in a DataTable

From Dev

LINQ To Datatable using "Any" with a List

From Dev

Loop datatable using linq expression

From Dev

How to query a single value from a datatable using linq in vb.net

From Dev

Multiple on clause in LINQ to DataTable Join Query

From Dev

LinQ query select DataRow from DataTable

From Dev

c# How to query Datatable by LINQ

From Dev

convert linq query results to datatable C#

From Dev

LINQ query on Datatable - Pulling multiple columns error

From Dev

How to convert a LINQ query result to a DataTable dynamically?

From Dev

C# LINQ query from datatable

From Dev

Query in Access using NOT LIKE

From Dev

Using Func<> in LINQ Query

From Dev

Using into and from in Linq query

From Dev

Linq search query using ||

From Dev

Using the results linq query in another linq query

From Dev

Linq 2 Sql Compare a local sequence to a query using 'Like' - most efficient method