From the course: .NET Essentials: Working with LINQ

What sources are queryable? - .NET Tutorial

From the course: .NET Essentials: Working with LINQ

What sources are queryable?

`- If we're going to query some data, we have to understand what kind of data sources we can query. The first thing to understand, a core principle, is that you have to have a pool of data to query. Now this pool can go by different names. In the functional programming world, it's often called a sequence. I called it a pool, you might call it a group of data. In DyNet, we have class names that represents this group of data. We might call it a collection or an array. It doesn't matter the name. The point is in the .net world in LINQ, that group of data has to implement a certain interface. There's actually two interfaces that it needs to implement. The one we're looking at in this course is IEnumerable of T. There's also one called IQueryable, which we'll get to in a minute. So I've got a query here. Don't worry too much about the syntax yet, but basically it's looking in one of the .net framework libraries called mscorlib and it's querying all the types that are in that library, that DLL, and finding out if they implement the IEnumerable of T interface. So if I run this, I get a list of all of the types of influences. So these are all potential sources of data for a LINQ query. Now, this is not all of the IEnumerable of T, it's just the ones that happen to be in this library. Now, if there's other types in other DyNet libraries or in third party libraries, those are also queryable as long as they implement the right interface. Let's take a look at what we got here. We have a collection. Now refresh your memory on this. This backtick character stands for errity, and that means how many generic type parameters it has. So this collection of T, concurrent dictionary has two type parameters. Dictionaries typically have a key and a value. There's concurrent Q, concurrent stack, there's a dictionary, there's some key collections, there's a list of T, and the one that you might not expect to see on this list is string. We'll talk more about that later, too. So these are some of the sources that you can query. There are other types of sources that implement IQueryable. This is for a whole 'nother category of queryable data. And this is one of the things that makes LINQ really powerful, but I'm not talking about it much in this course. Queryable providers are ones that usually have their own separate querying languages. So you're basically doing a mapping between LINQ and the query provider. A classic example of this is a SQL server database. It uses SQL to query. So when you write a LINQ query, what's really happening, is that's getting translated into the correct SQL statements to talk to the database and that's done through, what's called a queryable provider. And that's a whole 'nother topic and you'll learn about that when you get into LINQ to Entities or LINQ to SQL and some of those other flavors of LINQ. But I thought I would go ahead and show you a couple of the types. So here's a couple that are in this assembly, System.Data.Entity. But again, for this course and the rest of this course, we're looking at LINQ to objects and that's the one, so you're looking for a pool of data that is implemented as IEnumerable of T.

Contents