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, etc.). LINQ allows developers to write queries directly in 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 linking LINQ extension methods together 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 => ) .OrderBy(item => ) .Select(item => );
2. Query syntax
Query syntax uses SQL-like query expressions in C# code. It is more declarative and similar to SQL queries, making it easier for developers familiar with SQL to understand and write queries.
var result = from item in collection where orderby select ;
The two syntaxes are functionally equivalent; they represent the same underlying operation and produce the same results. Developers can choose their preferred syntax based on readability, personal preferences, or the nature of the query.
Types of LINQ
- LINQ to Objects: used to query data structures in memory, such as collections, arrays, lists, etc.
- LINQ to XML (XLINQ): This is used to query XML data sources using the LINQ syntax.
- LINQ to SQL: This is used to query relational databases using the LINQ syntax. It converts LINQ queries into SQL queries to interact with the database.
- LINQ to Entities: This is similar to LINQ to SQL, but is used with the Entity Framework to query the database using the LINQ syntax. It uses a conceptual entity data model instead of using database tables directly.
- LINQ to Dataset: This is used to query datasets using the LINQ syntax.
- LINQ to JSON (): Although not part of the official LINQ framework, libraries like this provide LINQ-like capabilities to query JSON data.
example
class Person { public string Name { get; set; } public int Age { get; set; } public string City { get; set; } }
You have the following Person object list:
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 from New York.
**Method Syntax: **This involves linking LINQ extension methods together to form a query.
var newYorkers = (p => == "New York");
**Query Syntax: **This involves using SQL-like query expressions.
var newYorkers = from p in people where == "New York" select p;
Example 2. Find the average age of people in Los Angeles.
Method syntax:
var averageAgeLosAngelesMethodSyntax = people .Where(p => == "Los Angeles") .Average(p => );
Query syntax:
var averageAgeLosAngelesQuerySyntax = (from p in people where == "Los Angeles" select ) .Average();
Example 3. Find the oldest person on the list.
Method syntax
Method syntax:
var oldestPersonMethodSyntax = (p => ).First();
Query syntax:
var oldestPersonQuerySyntax = (from p in people orderby descending select p).First();
The two syntaxes are functionally equivalent; they represent the same underlying operation and produce the same results. Developers can choose their preferred syntax based on readability, personal preferences, or the nature of the query.
This is all about this article about LINQ: Syntax and Types in C#. For more related C# LINQ syntax and type content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!