LINQ (Language-Integrated Query) is a feature introduced in C# that provides a unified way to query data from different types of data sources such as collections, arrays, XML, databases, and more. LINQ allows developers to write queries directly within C# code, making it easier to manipulate and transform data.
There are two main syntaxes for writing LINQ queries.
1. Method Syntax
Method syntax involves chaining together LINQ extension methods to form a query. Each LINQ operation is represented by a method call, such as Where, Select, OrderBy, Join, etc.
var result = collection
.Where(item => item.Condition)
.OrderBy(item => item.Property)
.Select(item => item.Transformation);
2. Query Syntax
Query syntax uses SQL-like query expressions within C# code. It's more declarative and resembles SQL queries, making it easier for developers familiar with SQL to understand and write queries.
var result = from item in collection
where item.Condition
orderby item.Property
select item.Transformation;
Both syntaxes are functionally equivalent; they represent the same underlying operations and produce the same results. Developers can choose the syntax they prefer based on readability, personal preference, or the nature of the query.
Types of LINQ
- LINQ to Objects: This is used to query in-memory data structures such as collections, arrays, lists, etc.
- LINQ to XML (XLINQ): This is used to query XML data sources using LINQ syntax.
- LINQ to SQL: This is used to query relational databases using LINQ syntax. It translates LINQ queries into SQL queries to interact with databases.
- LINQ to Entities: This is similar to LINQ to SQL but is used with Entity Framework to query databases using LINQ syntax. It works with conceptual entity data models rather than directly with database tables.
- LINQ to Dataset: This is used to query datasets in ADO.NET using LINQ syntax.
- LINQ to JSON (JSON.NET): While not a part of the official LINQ framework, libraries like JSON.NET provide LINQ-like capabilities for querying JSON data.
Examples
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
}
You have a list of Person objects as follows:
List<Person> people = new List<Person>
{
new Person { Name = "Alice", Age = 25, City = "New York" },
new Person { Name = "Bob", Age = 30, City = "Los Angeles" },
new Person { Name = "Charlie", Age = 35, City = "Chicago" },
new Person { Name = "David", Age = 40, City = "New York" },
new Person { Name = "Emma", Age = 45, City = "Los Angeles" },
};
Example 1. Find all people who are from New York.
Method Syntax: This involves chaining LINQ extension methods together to form a query.
var newYorkers = people.Where(p => p.City == "New York");
Query Syntax: This involves using SQL-like query expressions.
var newYorkers = from p in people
where p.City == "New York"
select p;
Example 2. Find the average age of people from Los Angeles.
Method Syntax
var averageAgeLosAngelesMethodSyntax = people
.Where(p => p.City == "Los Angeles")
.Average(p => p.Age);
Query Syntax
var averageAgeLosAngelesQuerySyntax = (from p in people
where p.City == "Los Angeles"
select p.Age)
.Average();
Example 3. Find the oldest person on the list.
Method Syntax
var oldestPersonMethodSyntax = people.OrderByDescending(p => p.Age).First();
Query Syntax
var oldestPersonQuerySyntax = (from p in people
orderby p.Age descending
select p).First();
Both syntaxes are functionally equivalent; they represent the same underlying operations and produce the same results. Developers can choose the syntax they prefer based on readability, personal preference, or the nature of the query.