C# Miscellaneous Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
You can use a LINQ query with the Where
method to filter a DataTable and return a single DataRow that matches a certain condition. Here's an example:
DataTable table = new DataTable(); // add columns and rows to the DataTable DataRow targetRow = table.AsEnumerable() .Where(row => row.Field<string>("Column1") == "Value1") .FirstOrDefault();
In the above example, we first create a new DataTable and fill it with data. We then use the AsEnumerable
method to convert the DataTable to an IEnumerable<DataRow>
. We use the Where
method to filter the rows that match the condition where Column1
equals "Value1". We then use the FirstOrDefault
method to return the first row that matches the condition, or null
if no such row exists.
You can use a LINQ query with the Where
method to filter a DataTable and return multiple DataRows that match a certain condition. Here's an example:
DataTable table = new DataTable(); // add columns and rows to the DataTable IEnumerable<DataRow> targetRows = table.AsEnumerable() .Where(row => row.Field<string>("Column1") == "Value1");
In the above example, we first create a new DataTable and fill it with data. We then use the AsEnumerable
method to convert the DataTable to an IEnumerable<DataRow>
. We use the Where
method to filter the rows that match the condition where Column1
equals "Value1". We then get the resulting IEnumerable<DataRow>
which contains all the rows that match the condition.
You can use a LINQ query with the Select
method to project a DataTable and return a sequence of specific column values. Here's an example:
DataTable table = new DataTable(); // add columns and rows to the DataTable IEnumerable<string> columnValues = table.AsEnumerable() .Select(row => row.Field<string>("Column1"));
In the above example, we first create a new DataTable and fill it with data. We then use the AsEnumerable
method to convert the DataTable to an IEnumerable<DataRow>
. We use the Select
method to project each row to its Column1
value. We then get the resulting IEnumerable<string>
which contains all the values of Column1
.
You can use the CopyToDataTable
extension method to fill a new DataTable with the result set of a LINQ query. Here's an example:
IEnumerable<DataRow> result = // LINQ query that returns an IEnumerable<DataRow> DataTable table = result.CopyToDataTable();
In the above example, we have a LINQ query that returns an IEnumerable<DataRow>
as a result set. We then use the CopyToDataTable
method to fill a new DataTable with the result set.
You can use a LINQ query with the Count
method to count the number of rows in a DataTable that match a certain condition. Here's an example:
DataTable table = new DataTable(); // add columns and rows to the DataTable int count = table.AsEnumerable() .Count(row => row.Field<string>("ColumnName") == "Value");
In the above example, we first create a new DataTable and fill it with data. We then use the AsEnumerable
method to convert the DataTable to an IEnumerable<DataRow>
. We use the Count
method to count the number of rows that match the condition where ColumnName
equals "Value". We then get the resulting int
value which represents the count of rows.
LINQ query on DataTable in C#
LINQ queries on DataTables in C# allow you to perform SQL-like operations on the data.
using System; using System.Data; using System.Linq; class Program { static void Main() { DataTable dataTable = GetDataTable(); var query = from row in dataTable.AsEnumerable() where row.Field<int>("Age") > 25 select row; foreach (var row in query) { Console.WriteLine($"Name: {row["Name"]}, Age: {row["Age"]}"); } } static DataTable GetDataTable() { DataTable dataTable = new DataTable(); dataTable.Columns.Add("Name", typeof(string)); dataTable.Columns.Add("Age", typeof(int)); dataTable.Rows.Add("John", 30); dataTable.Rows.Add("Alice", 22); dataTable.Rows.Add("Bob", 28); return dataTable; } }
Filtering DataTable using LINQ in C#
Use LINQ to filter rows in a DataTable based on a condition.
var filteredRows = from row in dataTable.AsEnumerable() where row.Field<int>("Age") > 25 select row;
Selecting specific columns with LINQ on DataTable in C#
Use LINQ to select specific columns from a DataTable.
var selectedColumns = from row in dataTable.AsEnumerable() select new { Name = row.Field<string>("Name"), Age = row.Field<int>("Age") };
Sorting DataTable with LINQ in C#
Sort rows in a DataTable using LINQ.
var sortedRows = from row in dataTable.AsEnumerable() orderby row.Field<int>("Age") select row;
Grouping and aggregating DataTable with LINQ in C#
Group and aggregate data in a DataTable using LINQ.
var groupedData = from row in dataTable.AsEnumerable() group row by row.Field<string>("Department") into departmentGroup select new { Department = departmentGroup.Key, AverageAge = departmentGroup.Average(r => r.Field<int>("Age")) };
Joining DataTables using LINQ in C#
Perform inner or outer joins on DataTables using LINQ.
var joinedData = from employee in employees.AsEnumerable() join department in departments.AsEnumerable() on employee.Field<int>("DepartmentID") equals department.Field<int>("ID") select new { EmployeeName = employee.Field<string>("Name"), DepartmentName = department.Field<string>("DepartmentName") };
Using LINQ to search and find rows in DataTable C#
Search and find specific rows in a DataTable using LINQ.
var foundRows = from row in dataTable.AsEnumerable() where row.Field<string>("Name") == "Alice" select row;
Conditional querying of DataTable with LINQ in C#
Apply conditional queries on a DataTable using LINQ.
var query = from row in dataTable.AsEnumerable() where row.Field<int>("Age") > 25 && row.Field<string>("Department") == "IT" select row;
Performing calculations on DataTable columns with LINQ in C#
Use LINQ to perform calculations on columns of a DataTable.
var totalAge = dataTable.AsEnumerable().Sum(row => row.Field<int>("Age"));
Combining LINQ queries with DataTable operations in C#
Combine LINQ queries with DataTable operations to achieve more complex tasks.
var result = from row in dataTable.AsEnumerable() where row.Field<int>("Age") > 25 orderby row.Field<string>("Name") select new { Name = row.Field<string>("Name"), Department = row.Field<string>("Department") }; // Further DataTable operations on 'result' var newDataTable = result.CopyToDataTable();