{"id":922,"date":"2023-03-13T14:58:23","date_gmt":"2023-03-13T07:58:23","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=922"},"modified":"2023-06-26T14:43:30","modified_gmt":"2023-06-26T07:43:30","slug":"what-is-linq","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-linq\/what-is-linq\/","title":{"rendered":"What is LINQ"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about what LINQ is, how it works, and why you should use it.<\/p>\n\n\n\n<p>In programming, you often deal with data manipulation. Whether it&#8217;s projecting, sorting, filtering, or grouping data, these operations can be complex and error-prone.<\/p>\n\n\n\n<p>Also, the data may come from various sources such as XML files, databases, third-party API, etc., which makes the data manipulation more complex. The reason is that you need to study a specific API for each data source.<\/p>\n\n\n\n<p>Fortunately, you can use a powerful tool in .NET that can make data manipulation easier: LINQ (Language Integrated Query).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to LINQ<\/h2>\n\n\n\n<p>LINQ is a set of language extensions in C# that allow you to manipulate and analyze data in a declarative and concise manner.<\/p>\n\n\n\n<p>LINQ allows you to write queries against various data sources using the common syntax and the same set of operators.<\/p>\n\n\n\n<p>LINQ syntax and operators are like SQL (structured query language), but LINQ offers a more type-safe and object-oriented approach to querying and managing data.<\/p>\n\n\n\n<p>LINQ leverages the following language features in C#:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-lambda-expression\/\">Lambda expressions<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-extension-methods\/\">Extension methods<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-anonymous-type\/\">Anonymous types<\/a><\/li>\n\n\n\n<li>Query expression syntax<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-generics\/\">Generics<\/a><\/li>\n\n\n\n<li><code>yield<\/code> and <code><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-var\/\">var<\/a><\/code> keywords<\/li>\n<\/ul>\n\n\n\n<p>LINQ allows you to write queries in two ways: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Query Syntax<\/li>\n\n\n\n<li>Method Syntax<\/li>\n<\/ul>\n\n\n\n<p>The query syntax uses a SQL-like syntax with keywords like from, <code>where<\/code>, <code>select<\/code>, <code>orderby<\/code>, <code>groupby <\/code>while the method syntax uses a set of extension methods such as <code><a href=\"https:\/\/csharptutorial.net\/csharp-linq\/linq-where\/\">Where()<\/a><\/code>, <code><a href=\"https:\/\/csharptutorial.net\/csharp-linq\/linq-select\/\">Select()<\/a><\/code>, <code><a href=\"https:\/\/csharptutorial.net\/csharp-linq\/linq-orderby\/\">OrderBy()<\/a><\/code>, and <code><a href=\"https:\/\/csharptutorial.net\/csharp-linq\/linq-groupby\/\">GroupBy()<\/a><\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Query syntax example<\/h3>\n\n\n\n<p>The following example shows how to use LINQ to query the even numbers from a list of numbers using the query syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n<span class=\"hljs-keyword\">var<\/span> numbers = <span class=\"hljs-keyword\">new<\/span> List&lt;<span class=\"hljs-keyword\">int<\/span>&gt; { <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span> };\n\n<span class=\"hljs-keyword\">var<\/span> evenNumbers = <span class=\"hljs-keyword\">from<\/span> number <span class=\"hljs-keyword\">in<\/span> numbers\n                  <span class=\"hljs-keyword\">where<\/span> number % <span class=\"hljs-number\">2<\/span> == <span class=\"hljs-number\">0<\/span>\n                  <span class=\"hljs-keyword\">select<\/span> number;\n\n<span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">var<\/span> number <span class=\"hljs-keyword\">in<\/span> evenNumbers)\n{\n    WriteLine(number);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define a list of integers called numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">var<\/span> numbers = <span class=\"hljs-keyword\">new<\/span> List&lt;<span class=\"hljs-keyword\">int<\/span>&gt; { <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span> };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, use the <code>from<\/code> keyword to define a range variable called <code>number<\/code> that represents each element in the <code>numbers<\/code> list, the <code>where<\/code> keyword to filter the range variable that selects only even numbers, and the <code>select<\/code> keyword to project the selected numbers into a new sequence.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">var<\/span> evenNumbers = <span class=\"hljs-keyword\">from<\/span> number <span class=\"hljs-keyword\">in<\/span> numbers\n                  <span class=\"hljs-keyword\">where<\/span> number % <span class=\"hljs-number\">2<\/span> == <span class=\"hljs-number\">0<\/span>\n                  <span class=\"hljs-keyword\">select<\/span> number;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, write all the selected even numbers to the console using a <code><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-foreach\/\">foreach<\/a><\/code> loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">var<\/span> number <span class=\"hljs-keyword\">in<\/span> evenNumbers)\n{\n    WriteLine(number);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-number\">2<\/span>\n<span class=\"hljs-number\">8<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Method syntax example<\/h3>\n\n\n\n<p>The following example rewrites the above program but uses the method syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n<span class=\"hljs-keyword\">var<\/span> numbers = <span class=\"hljs-keyword\">new<\/span> List&lt;<span class=\"hljs-keyword\">int<\/span>&gt; { <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span> };\n\n<span class=\"hljs-keyword\">var<\/span> evenNumbers = numbers\n                    .Where(number =&gt; number % <span class=\"hljs-number\">2<\/span> == <span class=\"hljs-number\">0<\/span>)\n                    .Select(number =&gt; number);\n\n<span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">var<\/span> number <span class=\"hljs-keyword\">in<\/span> evenNumbers)\n{\n    WriteLine(number);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the program chains the <code><a href=\"https:\/\/csharptutorial.net\/csharp-linq\/linq-where\/\">Where()<\/a><\/code> and <code><a href=\"https:\/\/csharptutorial.net\/csharp-linq\/linq-select\/\">Select()<\/a><\/code> methods to select even numbers from the numbers list.<\/p>\n\n\n\n<p>LINQ offers a useful set of operators for querying and manipulating data. The following are the most common ones:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><a href=\"https:\/\/csharptutorial.net\/csharp-linq\/linq-where\/\">Where()<\/a><\/code> &#8211; filter a sequence based on a condition.<\/li>\n\n\n\n<li><code><a href=\"https:\/\/csharptutorial.net\/csharp-linq\/linq-select\/\">Select()<\/a><\/code> &#8211; projects each member of a sequence into a new form.<\/li>\n\n\n\n<li><code><a href=\"https:\/\/csharptutorial.net\/csharp-linq\/linq-orderby\/\">OrderBy()<\/a><\/code> \/ <code><a href=\"https:\/\/csharptutorial.net\/csharp-linq\/linq-orderbydescending\/\">OrderByDescending()<\/a><\/code> &#8211; sort the sequence in ascending or descending order based on a sort key.<\/li>\n\n\n\n<li><code><a href=\"https:\/\/csharptutorial.net\/csharp-linq\/linq-groupby\/\">GroupBy()<\/a><\/code> &#8211; group the elements based on a key.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">LINQ advantages<\/h2>\n\n\n\n<p>LINQ offers the following benefits:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Expressiveness: LINQ allows you to write declarative and concise code for querying data, making it easier to understand and maintain.<\/li>\n\n\n\n<li>Type safety: LINQ queries are strongly typed. This means that the compiler will catch type errors at compile time, which reduces the errors that can discover at runtime only.<\/li>\n\n\n\n<li>Integrate objects, relational data, and XML files &#8211; you can use unified query syntax across data sources. <\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>LINQ stands for Language Integrated Query.<\/li>\n\n\n\n<li>LINQ provides a unified API for manipulating data across data sources including objects, relational databases, and XML files.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"922\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-linq\/what-is-linq\/\"\n\t\t\t\tdata-post-title=\"What is LINQ\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"922\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-linq\/what-is-linq\/\"\n\t\t\t\tdata-post-title=\"What is LINQ\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about what LINQ is, how it works, and why you should use it.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":919,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-922","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=922"}],"version-history":[{"count":4,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/922\/revisions"}],"predecessor-version":[{"id":2450,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/922\/revisions\/2450"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/919"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}