LINQ (Language Integrated Query) is a powerful feature in C# that allows developers to work with data in a more efficient way. In this tutorial, we’ll be taking a deep dive into how to use LINQ with DataTables in C# , using .NET 6. We’ll be creating a console application in Visual Studio to demonstrate the process, and also provide a breakdown of the code so that readers can understand how it works
By the way, if you prefer a video format, check out the video to this article on our dedicated C# channel HERE!
Creating a Console Project in Visual Studio
- Open Visual Studio and click on “Create a new project.”

- Select “Console App (.NET 6)” from the list of templates.

- Give your project a name, for example “LinqWithDataTables” and click on “Create.”

Adding a DataTable to the Project
- In Solution Explorer, right-click on the project and select “Add” and then “class”
- Name the class “Employees” and click on “Add.”
In the new class, we can make it inherit from the DataTable class, and define the structure of the table by adding DataColumn objects to the Columns collection of the DataTable.
class Employees : DataTable
{
public Employees()
{
this.Columns.Add("Id", typeof(int));
this.Columns.Add("Name", typeof(string));
this.Columns.Add("Age", typeof(int));
this.Columns.Add("Department", typeof(string));
this.Columns.Add("Salary", typeof(int));
}
}
This class will be used to define the structure of the DataTable, i.e the columns and their data types.
Defining the Structure of the DataTable
In the Employees.cs file, you can define the columns for the DataTable by adding new DataColumn objects to the Columns collection of the DataTable.
public Employees()
{
this.Columns.Add("Id", typeof(int));
this.Columns.Add("Name", typeof(string));
this.Columns.Add("Age", typeof(int));
this.Columns.Add("Department", typeof(string));
this.Columns.Add("Salary", typeof(int));
}
In this example, we are adding 5 columns to the DataTable, Id, Name, Age, Department and Salary, all with different data types.
Populating the DataTable with Data
In the Main method of the Program.cs file, create an instance of the Employees DataTable.
var employees = new Employees();
Use the Rows.Add method to add rows to the DataTable. You can add some sample data to the DataTable for demonstration purposes.
employees.Rows.Add(1, "John Smith", 30, "IT", 5000);
employees.Rows.Add(2, "Jane Doe", 25, "HR", 4000);
employees.Rows.Add(3, "Bob Johnson", 35, "IT", 6000);
employees.Rows.Add(4, "Samantha Williams", 28, "Marketing", 4500);
How to use LINQ in Datatables in C#
Import the System.Linq namespace at the top of the Program.cs file.
using System.Linq;
Use the LINQ query syntax to filter, sort, and group the data in the DataTable. For example, you can use the Where method to filter the data based on a certain condition, the OrderBy method to sort the data, and the GroupBy method to group the data.
Here is an example of how you can use LINQ to query the data in the DataTable:
var query = from employee in employees.AsEnumerable() where employee.Field<string>("Department") == "IT" orderby employee.Field<int>("Salary") descending select new { Id = employee.Field<int>("Id"), Name = employee.Field<string>("Name"), Age = employee.Field<int>("Age"), Department = employee.Field<string>("Department"), Salary = employee.Field<int>("Salary") };
This LINQ query will filter the data in the DataTable to only include employees who work in the “IT” department, sort the data by the employee’s salary in descending order, and select the employee’s id, name, age, department and salary as the fields to be displayed.
Here is an example of how you can display the results of the LINQ query:
foreach (var item in query)
{
Console.WriteLine("Id: {0}, Name: {1}, Age: {2}, Department: {3}, Salary: {4}", item.Id, item.Name, item.Age, item.Department, item.Salary);
}
Conclusion: How to use LINQ in Datatables in C#
That’s it! You’ve just learned how to use LINQ with DataTables in C# using .NET 6. We’ve created a console application in Visual Studio, added a DataTable, defined its structure, populated it with data, and used LINQ to query the data in the DataTable. Remember that LINQ is a powerful tool that can help you work with data in a more efficient way. The full code for the project is provided below for reference.
// Main Class class Program { static void Main(string[] args) { // Create an instance of the Employees DataTable var employees = new Employees(); // Add rows to the DataTable employees.Rows.Add(1, "John Smith", 30, "IT", 5000); employees.Rows.Add(2, "Jane Doe", 25, "HR", 4000); employees.Rows.Add(3, "Bob Johnson", 35, "IT", 6000); employees.Rows.Add(4, "Samantha Williams", 28, "Marketing", 4500); // Use LINQ to query the DataTable var query = from employee in employees.AsEnumerable() where employee.Field<string>("Department") == "IT" orderby employee.Field<int>("Salary") descending select new { Id = employee.Field<int>("Id"), Name = employee.Field<string>("Name"), Age = employee.Field<int>("Age"), Department = employee.Field<string>("Department"), Salary = employee.Field<int>("Salary") }; // Display the results of the LINQ query foreach (var item in query) { Console.WriteLine("Id: {0}, Name: {1}, Age: {2}, Department: {3}, Salary: {4}", item.Id, item.Name, item.Age, item.Department, item.Salary); } Console.ReadLine(); } } //Employees Datatable Class using System.Data; class Employees : DataTable { public Employees() { this.Columns.Add("Id", typeof(int)); this.Columns.Add("Name", typeof(string)); this.Columns.Add("Age", typeof(int)); this.Columns.Add("Department", typeof(string)); this.Columns.Add("Salary", typeof(int)); } }
You can now use this code as a starting point and add more features to make it more useful for your specific use case. For example, you can add more columns to the DataTable, or use different LINQ methods and operators to query the data. Feel free to experiment and explore different ways to work with data using LINQ and DataTables in C#.
If you’re new to C# and collections, it’s always a good idea to start with basic examples and then build on them as you gain more experience. And don’t forget that when in doubt, you can always refer to this article to help you understand how to use LINQ in Datatables C#.
If you want to skyrocket your C# career, check out our powerful ASP.NET FULL-STACK WEB DEVELOPMENT COURSE which also covers test-driven development and C# software architecture.