From the course: .NET Essentials: Working with LINQ

Learn how to use MoreLinq for extended query syntax - .NET Tutorial

From the course: .NET Essentials: Working with LINQ

Learn how to use MoreLinq for extended query syntax

- [Instructor] All of the extension methods you looked at in this course have been part of the IEnumerable type. We know that they're implemented as extension methods and most of them are implemented as extension methods that extend IEnumerable<T>. Now if you know that, and you know how to write your own custom extension methods, you could create similar queries. Microsoft's done a good job of thinking about the most common operations that you'd want to do in a query language, but there are possibly operations that you need that aren't in LINQ. So that's what we're going to look at in this video and the next video, is how to work with custom extension methods that enhance LINQ. Now I'm not showing you how to write your own. But instead, we're going to take a look at a package by a third party called MoreLINQ. You can find it at morelinq.github.io and you can find examples on github.com/morelinq and it contains a lot of extra features. The first thing we need to do is add it to our project, so the way we're going to do that is get it through a NuGet package. In Visual Studio, you would use the Package Manager to do that; in LINQPad, you can right click and choose NuGet Package Manager or References and Properties. Choose to add NuGet. Search online for MoreLINQ like this. Find it; there'll be a button here to add it to your query editor in LINQPad. I've already done that. You can see it's over here. So I want to close this dialogue. You see that I've got courselive.dll and MoreLINQ from NuGet available. And now we'll start working with some of the examples. In this video will look at just one example. It's called the Move extension method. I'm looking at the MoreLINQ GitHub example page and here is the entry for Move. It says "it removes a sequence with a range of elements "in the source sequence, moved to a new offset." So it allows us to reorder the contents of the sequence without having to write a bunch of nested OrderBy clauses. So let's see what the syntax looks like. I start with a list of numbers from one to seven, and then we'll take a look at the signature for this method. It's got a from index, the number of items you want to move, and the to index, so for this first example, I am moving three positions. I'm taking all of the numbers in the sequence, and I'm moving them to the beginning of this new sequence. So what do I get? We take four, five, six, and seven, and we move them to the beginning like this, and then one, two, and three get placed at the end of the sequence. For the second example, I'm moving four; I'm moving all of the numbers and I'm moving them to the second position. So I get one, two, five, six, seven, three, four. For this, again, as I said earlier, this is a nice way of extending LINQ, one, and two; this Move method is a nice way of reordering the contents without having to figure out how to write a complex OrderBy.

Contents