Data Collections Using LINQ

Hey guys, here we'll talk about the use of LINQ. LINQ means language integrated query, which basically was established looking at the needs of database developers. While writing databases you need to have a specific knowledge of database language, but what Microsoft has done isto make things easier for developers. They have put almost the same syntax as in C# so that developers don't have to change their platform and go here and there again and again. One can simply write it down in the same page using LINQ.

In the code here, I've used the entries in the list and then I've added the values to it. We provide the values as in a table in SQL SERVER MANAGEMENT studio. But this has been made easy for us so that we can only use C# code to add on values in tables. We write queries as in a database. For instance, we have inserted values in student table as shown in the code and then afterwards I've written code which is attached here as a picture. Now that we've inserted the values we can write the queries for OrderBy values as,
  1. Var ordered = from Student in myList  
  2. orderby Student.year descending  
  3. select Student;  
What this query is going to do is that it'll take from students which are in MyList and arrange the students by year in descending order, and will select those student having year in their attribute filled.

Now for the display of the value I've used foreach loop which will select from students in the ordered list, and you can display them by writing the right code.

One more thing that is used here is ordered Var type variables which basically select the datatype from the compiler, which lets compiler select the value of the coming input and then change its type accordingly. It can have multiple type values as well. Like it can hold on to instances which have multiple types. For example it can carry int, string, and string all at one time.

Furthermore, you can refer to the pictures of code attached and for even more reference you can refer to Microsoft Virtual Academy (Fundamentals of C#) and then to the video tutorial of Robert Bob Taybor, and in there you can refer to lecture 23 for a more clear understanding.

Kindly find the link attached for video tutorials.

Ebook Download
View all
Learn
View all