Skip to content

Latest commit

 

History

History
105 lines (86 loc) · 2.63 KB

LINQ.md

File metadata and controls

105 lines (86 loc) · 2.63 KB

LINQ

Simple Explanation

LINQ (Language-Integrated Query) is a component of C# and .NET that provides a way to query data sources using a programming language. It allows developers to use familiar syntax to retrieve data from databases, XML files, or any other data source that can be queried. LINQ also provides a way to manipulate data using the same syntax.

Deep Explanation

LINQ is a technology that allows developers to query data in C# using a familiar syntax. It provides a set of standard query operators that can be used to retrieve data from any data source that can be queried, such as databases, XML files, and collections. The query operators are defined as extension methods on the IEnumerable interface, which allows them to be used with any collection that implements this interface.

LINQ uses a combination of lambda expressions and extension methods to provide a powerful and flexible querying mechanism. The lambda expressions are used to define the query, while the extension methods are used to perform the actual query. The result of a LINQ query is typically a new sequence of data that has been transformed in some way.

LINQ also provides a way to manipulate data using the same syntax. This allows developers to easily perform operations such as sorting, filtering, and grouping on collections of data. The LINQ syntax is concise and easy to read, which makes it a popular choice for many developers.

Examples:

Here are some basic examples of LINQ queries in C#

1 - Querying a collection:

int[] numbers = { 1, 2, 3, 4, 5 };
var query = from num in numbers
            where num % 2 == 0
            select num;

foreach (var num in query)
{
    Console.WriteLine(num);
}

/*
2
4
*/

2 - Querying a database:

var query = from customer in db.Customers
            where customer.City == "London"
            select customer;

foreach (var customer in query)
{
    Console.WriteLine(customer.ContactName);
}

3 - Querying an XML file:

var query = from element in xml.Elements("book")
            where element.Element("author").Value == "Stephen King"
            select element;

foreach (var element in query)
{
    Console.WriteLine(element.Element("title").Value);
}

Most commonly used extension methods from LINQ

  • Aggregate
  • All
  • Any
  • AsEnumerable
  • Average
  • Cast
  • Concat
  • Contains
  • Count
  • DefaultIfEmpty
  • Distinct
  • ElementAt
  • Except
  • First
  • GroupBy
  • Intersect
  • Join
  • Last
  • Max
  • Min
  • OfType
  • OrderBy
  • Range
  • Repeat
  • Reverse
  • Select
  • SelectMany
  • SequenceEqual
  • Single
  • Skip
  • SkipWhile
  • Sum
  • Take
  • TakeWhile
  • ThenBy
  • ToDictionary
  • ToList
  • ToLookup
  • Union
  • Where
  • Zip