ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Filter List Using Linq in C# and VB.Net<p>Hi <a class="username" rel="sanvi"> sanvi</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">Name : &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtName&#34; AutoPostBack=&#34;True&#34; OnTextChanged=&#34;Search&#34; /&gt; &lt;br /&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;gvCustomers&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;CustomerId&#34; HeaderText=&#34;CustomerId&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;GridView1&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { List&lt;Customers&gt; customes = ListOfCustomers(); gvCustomers.DataSource = customes; gvCustomers.DataBind(); } } private static List&lt;Customers&gt; ListOfCustomers() { List&lt;Customers&gt; customes = new List&lt;Customers&gt; { new Customers { CustomerId = 1, Name = &#34;John Hammond&#34;,Country=&#34;United States&#34; }, new Customers { CustomerId = 2, Name = &#34;Mudassar Khan&#34; ,Country=&#34;India&#34;}, new Customers { CustomerId = 3, Name = &#34;Robert&#34;,Country=&#34;France&#34; }, new Customers { CustomerId = 4, Name = &#34;Suzanne Mathews&#34; ,Country=&#34;Russia&#34;}, }; return customes; } protected void Search(object sender, EventArgs e) { List&lt;Customers&gt; customes = ListOfCustomers(); var query = customes.Where(x =&gt; x.Name == txtName.Text).Select(fetch =&gt; new Customers { Name = fetch.Name }); GridView1.DataSource = query; GridView1.DataBind(); } public class Customers { public int CustomerId { get; set; } public string Name { get; set; } public string Country { get; set; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim customes As List(Of Customers) = ListOfCustomers() gvCustomers.DataSource = customes gvCustomers.DataBind() End If End Sub Private Shared Function ListOfCustomers() As List(Of Customers) Dim customes As List(Of Customers) = New List(Of Customers) From { New Customers With { .CustomerId = 1, .Name = &#34;John Hammond&#34;, .Country = &#34;United States&#34; }, New Customers With { .CustomerId = 2, .Name = &#34;Mudassar Khan&#34;, .Country = &#34;India&#34; }, New Customers With { .CustomerId = 3, .Name = &#34;Robert&#34;, .Country = &#34;France&#34; }, New Customers With { .CustomerId = 4, .Name = &#34;Suzanne Mathews&#34;, .Country = &#34;Russia&#34; } } Return customes End Function Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs) Dim customes As List(Of Customers) = ListOfCustomers() Dim query = customes.Where(Function(x) x.Name = txtName.Text).[Select](Function(fetch) New Customers With { .Name = fetch.Name }) GridView1.DataSource = query GridView1.DataBind() End Sub Public Class Customers Public Property CustomerId As Integer Public Property Name As String Public Property Country As String End Class</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/HzSGpz9.gif" alt="" width="313" height="347" /></p>https://www.aspforums.net:443/Threads/149948/Filter-List-Using-Linq-in-C-and-VBNet/https://www.aspforums.net:443/Threads/149948/Filter-List-Using-Linq-in-C-and-VBNet/Mon, 16 Sep 2019 04:46:17 GMT