LINQ queries to DataTable in C#: ultimate guide with examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with DataTable via LINQ: from beginner to master in 20 minutes

\n\n

Imagine a situation: you are writing a C# application, loading data from a database into a DataTable, and then… you need to filter rows by a complex condition, group by date, or join two tables. The good old DataTable.Select("...") with its string filters breaks on the second condition, and foreach loops turn the code into spaghetti.

\n\n

Sound familiar? Then welcome to the world of LINQ queries to DataTable. This is your super-tool that turns working with tabular data into elegant, readable, and powerful code. In this guide, we'll cover everything: from basic Where to advanced Join and GroupBy. Let's go!

\n\n
\n\n

What is LINQ to DataSet and why do you need it?

\n\n

LINQ (Language Integrated Query) is a query language built into C#. Its main feature: you write queries to any data (arrays, lists, XML, databases) in the same way — using familiar operators from, where, select.

\n\n

But with DataTable there is a nuance: you cannot apply LINQ directly to it. DataTable does not implement the IEnumerable<T> interface. The solution is the AsEnumerable() method, which converts the table into a collection of DataRow objects. This is what LINQ works with.

\n\n

Advantages of the approach:

\n
    \n
  • ✅ Type safety: the compiler catches errors in column names
  • \n
  • ✅ Readability: the code looks like SQL but remains C#
  • \n
  • ✅ Power: filtering, projection, grouping, sorting — in one line
  • \n
  • ✅ Performance: deferred execution (doesn't load unnecessary data)
  • \n
\n\n

Let's get straight to the point. Add a reference to System.Data.DataSetExtensions in your project (usually already there) — and go ahead.

\n\n
\n\n

Data preparation: our test table

\n\n

To not be unfounded, let's create a DataTable with product data. The code below is our base for all examples:

\n\n
// 1. Create a table with columns\nDataTable products = new DataTable("Products");\nproducts.Columns.Add("Id", typeof(int));\nproducts.Columns.Add("Name", typeof(string));\nproducts.Columns.Add("Category", typeof(string));\nproducts.Columns.Add("Price", typeof(decimal));\nproducts.Columns.Add("DateAdded", typeof(DateTime));\n\n// 2. Add rows\nproducts.Rows.Add(1, "ASUS Laptop", "Electronics", 55000, DateTime.Now.AddDays(-10));\nproducts.Rows.Add(2, "Logitech Mouse", "Electronics", 2500, DateTime.Now.AddDays(-5));\nproducts.Rows.Add(3, "Book 'C# in Depth'", "Books", 1800, DateTime.Now.AddDays(-2));\nproducts.Rows.Add(4, "Sony Headphones", "Electronics", 12000, DateTime.Now.AddDays(-1));\nproducts.Rows.Add(5, "Moleskine Notebook", "Stationery", 1200, DateTime.Now.AddDays(-20));\nproducts.Rows.Add(6, "Parker Pen", "Stationery", 3500, DateTime.Now.AddDays(-15));\nproducts.Rows.Add(7, "Dell Monitor", "Electronics", 32000, DateTime.Now.AddDays(-30));
\n\n

Now we have 7 products. Time to apply the magic of LINQ.

\n\n
\n\n

Basic LINQ queries to DataTable

\n\n

1. Filtering (Where) — like SQL WHERE

\n\n

Suppose you need to select all products more expensive than 10,000 rubles. Without LINQ, you would write a loop checking row["Price"]. With LINQ it looks like this:

\n\n
var expensiveProducts = products.AsEnumerable()\n    .Where(row => row.Field<decimal>("Price") > 10000);\n\nforeach (var row in expensiveProducts)\n{\n    Console.WriteLine($"{row["Name"]} - {row["Price"]} rub.");\n}\n// Output: ASUS Laptop - 55000, Sony Headphones - 12000, Dell Monitor - 32000
\n\n

Important point: use the Field<T>("ColumnName") method instead of direct casting (decimal)row["Price"]. This is safer: if the column is DBNull, Fie

Blogs

Book Recommendations