DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Getting to Know LINQ

Getting to Know LINQ

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

Simon Foster user avatar by
Simon Foster
·
Oct. 10, 16 · Tutorial
Like (3)
Save
Tweet
Share
2.79K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Database

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Best CI/CD Tools for DevOps: A Review of the Top 10
  • Microservices 101: Transactional Outbox and Inbox
  • Introduction to Spring Cloud Kubernetes
  • How We Solved an OOM Issue in TiDB with GOMEMLIMIT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: