Getting Started Guide
To get started, just feel free to browse the various Linq operations that you can do using the left side menu. There is no specific order
What is Linq?
Linq stands for 'Language Integrated Query'. What does this mean? It means its a type of querying language used in .NET to query data, objects or whatever it can be applied to. A querying language? Yeah, take for example a simple "for" loop. Nothing special right? Now let's say you wanted to
It's kinda like SQL
Linq has some resemblance to SQL. But it's not exactly the same.
There are various forms of Linq such as Linq to SQL, Linq to XML and Linq to Objects. Here, we will be focusing on Linq to Objects.
Who is this designed for?
This is designed for all levels of C# developers ranging from beginners to those who are more familiar with Linq. It most cases, this website is useful as a reference as well as exploring different examples.
Why Linq and what are the benefits?
Well, there will be times when you have a set of data and you will need to filter or perform particular operations on the data set. You may need to find specific items in a list or may need to group data items based on a common attribute. These will generally involve iterating through a data set and carrying out the relevant actions.
Now, there are several solutions to this and it helps if the logic is performant, efficient and maintainable.
This is where Linq comes in. How? Because with perhaps one or two Linq statements, you can save a good few lines of code by avoiding writing for loops (not that they're a bad thing) complex nesting and for loop breaking.
With Linq, it is possible to write cleaner, readable and maintainable code.
There is of course over complex Linq statements and queries, and even myself I've seen some and it does me a moment to understand, but that's another topic.
Syntax - the Method and Query Syntax
If you're getting started in Linq, you may have come across two variations or two styles. Both generally achieve the same thing. You may have seen Linq written in these different ways and we'll go through both. Firstly, Linq can be written as a method syntax using an extension method combined with a lambda expression like so:
var result = list.Where(x=> x > 10)
The Where
method is the extension method (you'll need to add the using statement for System.Linq
in your code) and the x=> x > 10
is the lambda expression.
The same can be written in a query syntax like so:
var result = from l in list
where l > 10
select l;
Both produce identical results.
Which should you use?
Whatever you use and which ever syntax is entirely up to you and your personal preference. Some may argue that one is better than the other, but in my opinion, I would always go with readability and maintainability. Ultimately, I don't think there is a right or wrong way.
There will be some examples where you may not see a query syntax version. In these cases, it's most likely because, the query syntax is not too different from the method syntax and generally, once you are familiar with the query syntax, at times, they can be both used in conjunction.
Support and Donations
All the content here is free for consumption. No hidden fees or payments. If however you are feeling generous and would like to donate and support me, feel free to buy me a cup of coffee! Every little helps!
Attribution
Logos used: