Welcome back.
In this video, I want to show you how you can filter items based on a condition.
And therefore we're going to use link.
So up here you can see that you have generics.
So system.collections generics.
This allows us to use lists in the first place.
And then there is a link which allows us to use lists in a more efficient way, or data in general in
a very efficient way.
And I'm going to show you how that works.
So I have this list of items here with bananas, apples, cherries and berries.
And now what I would like to figure out are all the products that are inexpensive.
So everything that I can get for less than a dollar, for example.
So how would I do that?
Well, the thing is, I can use this thing called products dot where.
So there is this where keyword which filters a sequence of values based on a predicate.
So again we have this predicate thing.
Well the thing is this predicate is going to be the same thing that we have seen before where we just
pass a lambda expression.
So let me show you what this does or how this works.
So I can just say p and then lambda p becomes p dot price greater or less than I would say less than
1.0.
And this here will return an enumerable which we can convert into a list.
So I can go ahead and convert that into a list like so without this for each.
So this by itself is a valid statement.
However we're not storing it in the list yet.
So let's add another list here.
So we're going to make it a list of products where I'm going to call this one cheap products.
And I'm going to store whatever is the result of this line.
So there is a lot going on in this line.
So let's break it down step by step.
So we're saying give me the products dot where this condition is met basically where this condition
is met.
So this p here is a product.
So you can hover over it and you can see that it even says product P.
So this where clause will give us a product because we are using it on the products list okay.
So products is a list of individual product items.
So if I'm using this Where clause and I'm using a predicate this Where clause will give me as a parameter
the product itself.
So p now is the product.
Then I'm saying okay give me the p where p price.
So p dot price.
And why do we get price.
Well because p.
So the product class has this price property.
This one right here this price property.
And here you see we have different prices for each item.
And I'm just saying give me the piece where the price is less than 1.0.
Now this by itself will not return a list.
It will return something called an I enumerable.
I enumerables are something for a later point, but what they do allow us to do is they allow us to
make a list out of it with this method called two list.
So I enumerables are an even more flexible, more generic way of lists.
So there are many different types of lists.
We've seen arrays, we've seen lists.
There are other types of collections.
They're not called lists, but they're all called collections to a degree.
Right.
So we humans would call them lists.
But in coding they are called collections.
So there are many different types of collections.
Lists and arrays are just two of those types.
I enumerables are one type of a list that is even more generic, that is more capable, so to speak.
So a list is type A?
Well, one of the types of I enumerables obviously it's a little more complex than that because I enumerable
is an interface and so forth.
But that's something we're going to cover once we really understand object oriented programming.
Part three okay.
So at this point what we can do, however, we can just say, okay, so what you're returning is an
I enumerable whatever thing.
I don't know what that is, but I do know that I can convert I enumerables into lists and I can store
them inside of lists objects.
So inside of this cheap products variable which is of type list product.
Because this.
Here is a list of product as we converted it into a list after it was an enumerable of type product.
So now we made a list out of it, out of type product and now this cheap product.
List will now contain all the products that are cheaper than the given price.
So what we can now do is we can now just say, give me all the cheap products and print them out.
So what I'm going to do is I'm going to change this for each loop where I'm saying available products
for less than $1.
And here, instead of going through the products list, I'm going to go through the cheap products list,
which is this new list.
Out of that, we filtered with the Where clause.
And now let me run this again and we will see that cheap products are apples for $0.8 and bananas for
$0.3, and that pretty much is one portion of link.
So this thing here, this link here, if you go into by the way this is quite interesting to see.
So if you go into your directives you can click into here.
Once you are at the very top of your file you can click on any of them and you will get to this auto
generated file called List app Dot global using drugs.
So what you can see here is that we are using the namespace or the directive called system.
We're using collections generic and we're using link.
Currently we're not using HTTP dot net.
No threading no tasks no input output.
We're using none of that.
We are only using those three right link collections.
Generic and system system is what we need for anything, right?
Without system we can't do anything.
This collections generic allows us to use our lists.
Okay.
So this list here is part of collections generic.
So you can see that here System.collections.generic.
Now this where clause, where does that come from.
Well if you click into it you find that it is under the namespace system dot link.
So link holds these things called I enumerables which we have seen this keyword before.
Right?
Just a minute ago or two minutes ago.
And in there you have this where clause.
This is where this where method comes from.
But there are so many things going on here.
It's like whoa, what is going on.
So many things.
Well the thing is, don't worry about it.
At the end of the course you're going to understand all of that stuff, okay?
But right now the only thing we need to know is how to use it.
Later on, we will will understand even more how it's under the hood, but we're just not there yet.
Okay, so generally speaking, now you understand where we are at.
We are using link for the first time, and I know it's a little early, but we're going to cover link
in a lot more depth later on because this is really important whenever you're working with databases.
Really important, super useful tool.
Okay.
But for now you know how you can filter your lists even with complex items, right?
Not just something simple like ints or doubles or strings even, but with actual data types that we
created ourselves with multiple properties of different data types.
Okay, so congratulations on getting so far.
And I'd say see you in the next video.