In this article I will explain with an example, how to use the LINQ to select specific columns from DataTable in C# and VB.Net.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The following HTML Markup consists of a DropDownList and a GridView. The DropDownList is assigned OnSelectedIndexChanged property and the AutoPostBack property is set to True.
Country:
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Country_Changed">
<asp:ListItem Text="" Value=""></asp:ListItem>
<asp:ListItem Text="United States" Value="United States"></asp:ListItem>
<asp:ListItem Text="India" Value="India"></asp:ListItem>
<asp:ListItem Text="France" Value="France"></asp:ListItem>
<asp:ListItem Text="Russia" Value="Russia"></asp:ListItem>
</asp:DropDownList>
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Linq;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Linq
Imports System.Configuration
Imports System.Data.SqlClient
SELECT specific columns from DataTable using LINQ in C# and VB.Net
A function named GetData is used to fetch the records from the Customers table and return them in the form of a DataTable.
The GetData function is called at two places, first inside the Page_Load event of the page and second inside the Country_Changed event handler which is triggered on the DropDownList SelectedIndexChanged event.
Inside the Country_Changed event handler, first the selected value of the Country is fetched from the DropDownList.
If the selected value is not empty, then DataTable records are filtered using the WHERE clause of LINQ and the results are used to populate the GridView control.
If the selected value is empty, then simply the DataTable is used to populate the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData();
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
protected void Country_Changed(object sender, EventArgs e)
{
string country = ddlCountry.SelectedItem.Value;
DataTable dt = this.GetData();
if (!string.IsNullOrEmpty(country))
{
var customers = from customer in dt.AsEnumerable()
where customer.Field<string>("Country") == country
select new
{
CustomerId = customer.Field<int>("CustomerId"),
Name = customer.Field<string>("Name"),
Country = customer.Field<string>("Country")
};
gvCustomers.DataSource = customers;
gvCustomers.DataBind();
}
else
{
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sql = "SELECT * FROM Customers";
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql))
{
cmd.Connection = conn;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
}
}
return dt;
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = Me.GetData()
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Protected Sub Country_Changed(sender As Object, e As EventArgs)
Dim country As String = ddlCountry.SelectedItem.Value
Dim dt As DataTable = Me.GetData()
Dim dataView As DataView = dt.DefaultView
If Not String.IsNullOrEmpty(country) Then
Dim customers = dt.AsEnumerable().Select(Function(customer) New With {
.CustomerId = customer.Field(Of Integer)("CustomerId"),
.Name = customer.Field(Of String)("Name"),
.Country = customer.Field(Of String)("Country")
}).Where(Function(customer) customer.Country = country)
gvCustomers.DataSource = customers
gvCustomers.DataBind()
Else
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Private Function GetData() As DataTable
Dim dt As New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim sql As String = "SELECT * FROM Customers"
Using conn As New SqlConnection(constr)
Using cmd As New SqlCommand(sql)
cmd.Connection = conn
Using sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
End Using
End Using
End Using
Return dt
End Function
Screenshot
Demo
Downloads