LINQ (Language Integrated Query) is a feature in .NET that allows you to query collections of data (such as arrays, lists, databases, XML, etc.) using a consistent syntax directly in C# (or any other .NET language). It enables querying data from different data sources in a declarative manner, without needing to write complex SQL-like queries or use different technologies for different types of data.
With LINQ, you can write queries directly in your C# code in a way that integrates naturally with the language. This results in cleaner, more readable, and more maintainable code. LINQ provides a set of methods and operators for querying and manipulating data from various sources.
LINQ to Objects allows you to query in-memory collections like arrays, lists, and other IEnumerable<T> types using LINQ syntax. This is the simplest form of LINQ, where the data is already available in memory, and you're applying queries directly to those collections.
You can write LINQ queries in two ways: using Query Syntax (similar to SQL) or Method Syntax (using LINQ extension methods).
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Sample collection of integers
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// LINQ Query Syntax
var evenNumbers = from n in numbers
where n % 2 == 0
select n;
foreach (var num in evenNumbers)
{
Console.WriteLine(num);
}
}
}
In the above code, the query from n in numbers where n % 2 == 0 select n finds all even numbers from the numbers collection.
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Sample collection of integers
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// LINQ Method Syntax
var evenNumbers = numbers.Where(n => n % 2 == 0);
foreach (var num in evenNumbers)
{
Console.WriteLine(num);
}
}
}
Here, the Where() method is used to filter even numbers from the list. You can chain multiple LINQ methods like Where(), Select(), OrderBy(), etc., to form complex queries.
Where(): Filters elements based on a condition.
var filtered = numbers.Where(n => n > 5);
Select(): Projects each element into a new form (mapping).
var squares = numbers.Select(n => n * n);
OrderBy(): Sorts the elements in ascending order.
var sorted = numbers.OrderBy(n => n);
GroupBy(): Groups elements based on a key.
var grouped = numbers.GroupBy(n => n % 2 == 0);
First(), Last(): Returns the first or last element in a collection.
var firstEven = numbers.First(n => n % 2 == 0);
Aggregate(): Applies a function to accumulate values.
var sum = numbers.Aggregate((x, y) => x + y);
Any(), All(): Checks if any or all elements satisfy a condition.
var anyEven = numbers.Any(n => n % 2 == 0);
LINQ to SQL is a feature in .NET that allows you to query relational databases using LINQ syntax. It enables you to work with SQL databases like SQL Server in an object-oriented manner by mapping database tables to classes, which simplifies data access and manipulation.
DataContext class is used to interact with the database and execute LINQ queries. It allows you to work with tables in a database like they are collections in C#.DataContext class that is connected to your database.using System;
using System.Linq;
using System.Data.Linq;
class Program
{
static void Main()
{
// Assuming you have a LINQ to SQL DataContext for the database (e.g., NorthwindDB)
var context = new NorthwindDataContext();
// Querying the database using LINQ to SQL
var customers = from customer in context.Customers
where customer.Country == "USA"
select customer;
foreach (var customer in customers)
{
Console.WriteLine($"Customer: {customer.CompanyName}");
}
}
}
In this example, the NorthwindDataContext represents the database, and the Customers table is queried to find all customers in the USA.
Both LINQ to Objects and LINQ to SQL allow developers to write cleaner, more efficient, and strongly-typed queries in their applications, improving readability and reducing errors in data handling.
Open this section to load past papers