Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting to Know LINQ

DZone's Guide to

Getting to Know LINQ

Dive into the use and syntax that can make LINQ such an attractive part of your arsenal.

· Database Zone
Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

What Is LINQ?

LINQ is an acronym for Language Integrated Query, which describes where it’s used and what it does. The Language Integrated part means that LINQ is part of programming language syntax. In particular, both C# and VB are languages that ship with .NET and have LINQ capabilities.

Image title

How Do I Use LINQ in My C# Code?

To use LINQ the first thing you need to do is add the LINQ using statement.

using System.Linq;

In your code, you need a datasource. For this example, I am going to use a simple array, but it can be anything, like SQL, XML, etc.

int[] data = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };


Next, you need a LINQ query. (Note: I know the Q in LINQ means query, so I have just written query query. If you are one of those people who hates seeing the phrase "PIN number," then you might not like this blog post.) A LINQ query is very similar to a T-SQL query, so if, like me, you are good with databases. this should make sense to you.

In T-SQL you can have:

SELECT num
FROM data
WHERE num = 1


In LINQ, this becomes:

var query =
from num in data
where num == 1
select num;


Finally, you need to do  something with the query you have written. I am just going to print the results of my query to the console.

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


What Other SQL-Like Syntax Can I Use?

In T-SQL, you can control ordering using ORDER BY. LINQ has a similar syntax: orderby.

orderby num descending


In T-SQL, you can use GROUP BY. To do something similar with LINQ, use:

group num by num.Type into type
select type


This now requires a change to your foreach loop so you can list what you are grouping by and what items are in that group:

foreach (var type in query)
{
    Console.Write(type.Key);
    foreach (var num in type)
    {
        Console.Write(num);
    }
}


JOINs

So you thought joining tables was a SQL Server only thing? Think again — you can do this in LINQ:

var joinquery =
from cust in customers
join prod in products on prod.CustomerId equals cust.Id
select new { ProductName = prod.Name, CustomerName = cust.CompanyName };


Conclusion

There are loads more LINQ functionality that you can use. While writing this blog, I found this site, which has loads of examples of different queries that you can write with LINQ.

This has inspired me to use LINQ more in my code and learn more about the different queries that could be written.

Create flexible schemas using dynamic columns for semi-structured data. Learn how.

Topics:
linq ,syntax ,queries ,database

Published at DZone with permission of Simon Foster, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}