Link Search Menu Expand Document

C Sharp LINQ

Language-Integrated Query (LINQ) refers to a series of technologies that integrate query capabilities directly into the C# programming language. Traditionally, searches against data have been represented as plain strings with no type checking or IntelliSense assistance at build time. Furthermore, for each type of data source (SQL databases, XML documents, multiple Web services, and so on), you must master a different query language. Like classes, methods, and events, a query is a first-class language construct in LINQ. Using language keywords and familiar operators, you create queries against tightly typed collections of objects. The LINQ technology family offers a consistent query experience for things (LINQ to Objects), relational databases (LINQ to SQL), and XML (LINQ to XML).

The query expression is the most evident “language-integrated” aspect of LINQ for a developer who constructs queries. Declarative query syntax is used to write query expressions, and query syntax allows you to execute filtering, sorting, and grouping actions on data sources with little code. Employ the same fundamental query expression patterns to transform data in SQL databases, ADO.NET Datasets, XML documents and streams, and.NET collections.

LINQ queries in C# may be written for SQL Server databases, XML documents, ADO.NET Datasets, and any collection of objects that implements the IEnumerable or generic IEnumerableT> interface. Third-party LINQ support is also available for many Web services and other database systems.

The query operation is depicted in detail in the following example.

The entire procedure consists of establishing a data source, specifying the query expression, and running the query in a foreach loop.

Example

class LINQQueryExpressions
{
    static void Main()
    {
        // Specify the data source.
        int[] scores = new int[] { 75, 62, 50, 40 };

        // Define the query expression.
        IEnumerable<int> scoreQuery =
            from score in scores
            where score > 50
            select score;

        // Execute the query.
        foreach (int i in scoreQuery)
        {
            Console.Write(i + " ");
        }
    }
}
// Output: 75 62

Overview of Query Expressions

  • Query expressions can query and transform data from any LINQ-enabled data source.
  • A single query, for example, can retrieve data from a SQL database and generate an XML stream as output.
  • Query expressions are simple to understand because they use several popular C# language elements. The variables in a query expression are all strongly typed, albeit, in many situations, the type can be inferred by the compiler.
  • A query is not run until the query variable is iterated through, like in a foreach command.
  • Query expressions are transformed to Standard Query Operator method calls at build time according to the C# standard.
  • Method syntax may represent any question that can be written using query syntax.
  • However, query syntax is usually more understandable and concise.
  • When writing LINQ queries, we advocate using query syntax wherever possible and method syntax when required.
  • Query phrases are frequently more understandable than method syntax similar expressions.

Other useful articles:


Back to top

© , C Sharp Online — All Rights Reserved - Terms of Use - Privacy Policy