Mayur
Mayur

Reputation: 79

How to filter list in asp.net/vb.net using linq

I have a list of class

How can I filter on some condition..I am applying following it is working when value gets exact match

        Dim result = obj.OfType(Of clsEmrItmMstr)().Where(Function(s) s.GenName Like         txtserach.Text)
        grddetails.DataSource = result
        grddetails.DataBind()

where "clsEmrItmMstr" is my class name and "GenName" is field in class

Upvotes: 4

Views: 11432

Answers (2)

Satpal
Satpal

Reputation: 133403

You can use Contains function

Dim result As dynamic = obj.OfType(Of clsEmrItmMstr)().Where(Function(s) s.GenName.Contains(txtserach.Text))
grddetails.DataSource = result
grddetails.DataBind()

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460138

Instead of the Like operator you could simply use String.Contains:

Dim result = obj.OfType(Of clsEmrItmMstr)().
    Where(Function(s) s.GenName.Contains(txtserach.Text))

With Like you need * as wildcard, so this should work:

Dim result = obj.OfType(Of clsEmrItmMstr)().
    Where(Function(s) s.GenName Like String.Format("*{0}*", txtserach.Text))

(assuming that you want to find all objects where the GenName contains the text entered in the TextBox)

Upvotes: 5

Related Questions