How To Use Lambda Expression in C#(CSharp) [Lambda Expression C# Examples]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to coding droplets request you to support us by subscribing the channel and liking the videos don't forget to click the bell icon so that you will get notified once we upload new videos we'll be uploading more video tutorials of latest technologies and techniques in software development so stay updated okay so now let us start the session [Music] so in this video we are going to check the lambda expressions that we can use in a list a list of objects so there are several number expressions uh we can use in a list of objects to do several tasks so let us check one by one so first of all i have created a model class here i have named sorry here i have created a folder named model and inside that we are having a model class so the class name is customer and we have two elements in customer model class one is customer code and another one is customer name so this is a console application a dot net c sharp console application so in this application now while loading the application i have given or i have created a variable in which i have given or i have created five different customers so for customer code i have given 1 0 1 1 0 2 1 0 3 1 0 4 1 0 5 different values and some dummy customer names [Music] now the first lambda expression what we are going to check is select so i'm just declaring a variable word result is equal to customer list dot select a such that a dot customer code to list so in lambda expression here we can mention a variable uh whatever we need so now i have given a so instead of that we can given you a customer so here also we have to provide the same so customers says that customer dot customer code so now here for this tutorial i'm using a ash.a dot customer code to list now the select command or the select expression lambda expression will select the customer code element from this list so now this will result uh this will return a list of string so it will only take the customer codes from this list now for showing that customer code what we can do is [Music] we can use now this is a list of string so in the same example i will show one more lambda expression that is four each so uh for each is a method to loop through the list so for each set a such that uh console dot right line of a okay now in order to you know not that in order to keep the application alive or otherwise the application will get closed so i'm just giving console.read live now if we run the application yeah the application is running now so you can see now it has selected one zero one one zero two one zero only the customer codes from this particular list so this is how we can choose a particular element from the or a particular value from the list okay now the next we are going to check find options so i'm just commenting out these things okay oh sorry i forgot to stop the application okay so we are having a method named find is such that a dot customer code here we can give a criteria customer code is equal to one zero sorry it is a string value so one zero three okay now this result data type is a customer so it will return a single customer object and we will get that result here so we can check now and just creating a debug point here now running the application so you can see when after executing this particular command we got a customer record and 103 so 103 the customer name is george yeah here we can see the customer name so find will return a single object from the list okay now i need to show one more thing so here instead of giving it like this customer code dot substring of zero comma one so substring of zero comma one means uh substring of start index comma length so start index we have given as zero and length as one so this means the first uh the first character or the first element of this string it it will return the first element of the string so we can see here for all the customer codes the first element is one okay now here i am giving one here so we can say that this particular criteria matches all the records okay so if i'm running this application now still you can see it has returned one customer object so that is the first one 101 john so the first one got matched and it has returned that particular object now let's take an example i'm giving 201 here for john it is 201 now if i am running this application here you can see now it has selected the second record so now in this total list the first one doesn't match our criteria second third fourth and fifth will match the criteria but the application has chosen the first record which matched the criteria and it won't match the criteria with the rest of the objects okay so this is how fight will work now let me give another method all right let me use another method work condition so word now we can see the verb method has uh returned four objects so that these are the four objects so verb will return all the objects which matches the criteria now this the return type is a enumerable so for understanding it easily i am converting it to list [Music] okay so now we can see yeah the first element the first object is uh one zero two peter and the second object is one zero three george and in that way it has written all the four objects which has matched the criteria so the verb method will check each and every records whether it matches the criteria and it will return the value or it will return the result but the find method will okay here we cannot use to list why because it will return a single object so find method will check one by one and if it got a try if it got a record with uh which matches the criteria immediately it will result it will return the object this is how fine and this is the difference between find and work condition okay now i need to show one method which we use very commonly in the application or in development skip method so skip is for skipping some values now i have given skip of 2 here and just converting it to list now i'm just running the application [Music] so here you can see now it returned three objects so the first one is one zero three this one and the second one will be one zero four and uh the third one will be one zero five so it has returned these three records and skipped the first two one as we have given skip of two so it has skipped the first two records and returned the rest of our rest of the objects okay now next for okay i'll do one thing i'm just commenting out all this uh what numbers is equal to new list of inked and here we have some numbers 10 20 30 40 50 60. okay that's enough so so now this is a list of integers here we have used the list of customer object a moral class which we have created here now currently we are using a list of integers now if i need to get the sum we can use numbers dot sum okay now if i'm running the application you can see it has returned the sum value now the same thing for uh here also we can use if we need to calculate the sum of a particular element for example in uh for customer what kind of individual variable okay total orders so here we have a new element named total orders for customer so i'm just hard coding it for example john has made a total order of okay they have made two orders john has made so it is an integer variable so two john has made two orders in total and peter made five orders in total and george made ten dollars david made three orders wins made only one order now if we need to get the sum of total orders of all the customers what we can do is customer list dot sum of is such that add a dot total order so this will return the sum of the total orders now let's check here you can see now it has returned 21 so 21 is the total number of orders from all the customers now another thing is um uh okay another method is max so if we need to get the maximum number of order made we can use this function so sorry uh max value okay so now this will return the maximum value in the total order uh field of the total order of all customers so 10 is the maximum yes we can see here 10 is the biggest value in total order now in the same way if we need to find out the mean minimum value we can give here min so this will return uh the minimum value so that is one yeah we can see a one is the minimum value available in total orders so this is how we can get max and the min values now the next i'm going to show average so here i am going to use average of total orders so average is 4.2 so there are five different customers and total orders are 21 so 21 by 5 is 4.2 okay now we need to check uh or i'm going to show how we can use join functions using lambda expressions so for that uh i'm creating one more model class let's say country okay so in country we can have two different elements one is a country code okay and another one country name okay now inside the customer model we are adding one more element uh we are just using country code here but uh country name we won't be adding here country name will be here in the country model class okay now uh i'm just creating a country list is equal to new list of country model class sorry new country so i'm just adding two countries for showing at about two countries is enough so first country code is us and country name is united states okay now next gb united kingdom or else we can just for an easier and for understanding easily we can just uk here united kingdom okay now let's say john is from us i'm just copying and pasting [Music] okay and uh george is from uk then prince is also from uk okay now i'm just removing all this now i'm just showing how we can join these two list or one is country list another one is customer list so what we need is we need to show the customer code customer name and the country name so we need to match these two list using the country code so country code is the common element in both list and according to that we need to match the elements and we need to show country code sorry customer code customer name and country name so i'm just giving the join function join list okay now we need to mention the key selector of the first list that is the customer list so in customer list the key selector is country code a dot country code now we need to show the key selector of the second list that is country list so in country list also the name is country code of the key element so b dot country code okay now a comma b such that new a dot so we need to show the customer code then a dot customer name then b dot country name this is how we need to show the result so [Music] now result dot now for uh printing the values in the console application i am just giving it like this console dot right line [Music] a dot customer code then we dot customer name then a dot country name so result.forage so it will write uh the customer code customer name and uh sorry customer name and country name here we have to give contribute of each records in the result list and finally i'm just giving console dot rate key [Music] okay [Music] so now you can see it has joined both list 201 john from united states so here using the country code it has joined and taken the country name from here and george is from uk united kingdom so the things has worked as expected now another type of joint that is group join so i'm just removing this result equal to [Music] country list here i'm using country [Music] as the first element to get joint so here group i'm using group join group join with customer list now in the same way uh first we have to create the key of outer outer list so that you see such that they don't country code so this is outer list is a country list now uh for the inner list we need to choose again the country code so that is also country code so in our list is the customer list actually now it says that b is equal to new to list so here i am just mentioning the country code a dot country name and uh i'm just giving a name customer list equal to b to list i will just show you how the result will be then only you'll okay then only you can understand it okay so the result will be okay there are two elements first one is for us so now i'm if i'm opening us uh it is showing country code and country name and inside that there is a customer list a new list and inside that list it will show three different customer objects so which are in us uh those customers are from u.s actually in the same way we have uk and inside uk again we can see you know a list of customers which is having two different customer objects george and prince who are from uk so this is how group join works okay hope you understood now uh just one more small thing uh just i need to show how to group the values so for that we don't need uh this list okay what so here what we are going to do is now in this list you can see uh we are having different country codes us uk okay so okay before grouping i will just show you how to take the distinct values so our result is equal to customer list dot distinct of yes sorry oh okay not like that customer list dot select is such that a dot customer code okay uh now i'll just show you the result here so now i will uh after that i will show how to use the distinct method so now what we have done is uh we are choosing the oh sorry not cust we don't need customer code we need country code here okay now it will list uh all the country codes of the customer list actually so we have two us one uk gain one us one uk the same order now i'm going to use distinct method here so distinct method will remove duplicate values now we will have only two values us and uk this is how we can use a distinct method okay now next we are going to check group functions so your result equal to customer list dot group by is such that a dot country code so we are grouping using the country code now here i just need to mention one small thing if you need to group by multiple fields or multiple elements what you can do is you can mention it like this instead of giving like this you can use a dot country code a dot uh for example total order in this way you can use uh multiple fields for grouping now anyway for here for this example we are just using one single field so we can give it straight away here now dot select is such that mu okay now we have group using country code so a dot key key is the grouped element so basically key is the country code so we can just name it like this country code is equal to a dot key now one more thing uh as i told before if you are grouping with multiple elements what you can do is uh it will come like for example we have country code and total order two fields here so then we can use a dot key dot country code and a dot key dot total order but now as we have only one single field or one single element we can directly give a dot key say a dot key will be the country code value okay [Music] now we can use okay i'm just declaring in your name here customer count equal to a dot count so now it will show how many customers from each country course i'll just show you the result see that record us three customers customer account equal to three and uk customer count equal to two here instead of count we can use sum of total orders okay so now it will show a total order okay total order value or total orders value i'm just keeping the name like this so now it will show the sum of total order element here this the sum of this particular value of each countries let's check let's check how it works yes see from uh us we have a total order 10 total orders and from uk we have 11 total orders okay so in the same way we have we can use average all the same functions like average max min everything we can use here so this is the average of each country and another thing what we can use [Music] i'll just show you max b such that sorry microsoft system loader so now it will show uh total order max value so yeah now we uh it will show the max value of each country goes so in us the max value is 5 and in uk the max value is 10 in the same way you can use min also [Music] okay friends so that's it for this video so these are the most commonly used uh lambda expressions in the list the process is what we can use in the list actually so hope you enjoyed the video so please like the video and thank you all
Info
Channel: Coding Droplets
Views: 2,536
Rating: undefined out of 5
Keywords: lambda expression c#, c# lambda, c# lambda function, linq lambda expression, lambda expression c# example, lambda expressions in c# entity framework linq, c# lambda expression tutorial, lambda expression in linq c# examples, linq and lambda expressions, linq lambda expression in c#, linq lambda, c# linq lambda, lambda operator c#, lambda statement c#, Linq and lambda expressions in c#, lambda expression c# tutorial, lambda expression c# unity, csharp, c# lists, c#
Id: HbE07Hk_h9s
Channel Id: undefined
Length: 31min 54sec (1914 seconds)
Published: Thu Mar 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.