简体   繁体   中英

Datatable.Select with Like in VB.net

I have a datatable where i am trying to do datatable.Select(Name Like '#%#') but getting error that invalid pattern(expecting result of a table with name col having #Mike#,#Brow#..). Using escape sequense dint for all items dint work fine too. Many suggest to use Linq - but am new to it. How can i do this filter with Linq from this datatable.

This is a sample of what i was trying to do..

 Dim dtSamp As Data.DataTable
    dtSamp = New Data.DataTable
    dtSamp.Columns.Add("Name")
    dtSamp.Columns.Add("Marks")

    Dim dr As DataRow
    dr = dtSamp.NewRow()
    dr.Item(0) = "AAA"
    dr.Item(1) = "50"
    dtSamp.Rows.Add(dr)
    dr = dtSamp.NewRow()
    dr.Item(0) = "#bbb#"
    dr.Item(1) = "60"
    dtSamp.Rows.Add(dr)
    dr = dtSamp.NewRow()
    dr.Item(0) = "ccc"
    dr.Item(1) = "44"
    dtSamp.Rows.Add(dr)

    Dim drResult As DataRow()
    drResult = dtSamp.Select("Name Like '#%#'")

    Dim dtOutPutTable As Data.DataTable
    dtOutPutTable = drResult.CopyToDataTable()

In the dtOutPutTable i was expecting 1 row ie, #bbb# in it.. but the Select function fails.

Generally LINQ queries works on data sources which implement the IEnumerable<T>/ IQueryable<T> Interface . But DataTable does not implement any of these. So we can not directly apply LINQ queries on a DataTable.

But DataTable class has an extension method called AsEnumerable which returns an IEnumerable collection of DataRow. So we can apply the AsEnumerable function on a DataTable and then play with some LINQ on the resulting collection.

var items=(from p in myDataTable.AsEnumerable()
                 select new { ID= p.Field<int>("ID").
                              Name=p.Field<string>("Name")
                           }).ToList();
var filtered=items.Where(x => x.Name.Contains("Mike"));

EDIT : Here is the VB.NET Version ( Disclaimer: I am not a VB.NET guy. but i could build this code without any error)

Dim items = (From p In myDataTable.AsEnumerable() 
               Select New With {.ID = p.Field(Of Integer)("ID"),
                                .Name = p.Field(Of String)("Name")}).ToList()

Dim filtered = items.Where(Function(x) x.Name.Contains("Mike")).ToList()

VB

Private Function likes(ByVal dt As DataTable, ByVal column As String, ByVal value As String)
    Dim result = dt.Clone()
    For Each row As DataRow In From row1 As DataRow In dt.Rows Where (row1(column).Contains(value))
        result.ImportRow(row)
    Next
    Return result
End Function

C#

private DataTable likes(ref DataTable dt, string column, string value)
 {
    DataTable result = dt.Clone();
    foreach (DataRow row in from row1 in dt.Rowswhere (row1(column).Contains(value))) {
        result.ImportRow(row);
    }
    return result;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM