Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Regular data structures. With LINQ, we can also perform database query operations. LINQ supports many methods and functions that are used for Data manipulation like Updating, deletion, Insertion, and etc.
LINQ ElementAtOrDefault
LINQ ElementAtOrDefault operator is used to search the element present in a data source. It searches the element based on the index value provided. If the element is found at a specified index, the element is returned. Otherwise, it returns empty by default.
Syntax:
input_source.ElementAtOrDefault(index_number);
Where input_source is the data source that can be an array or List or any collection.
We will understand this by seeing the following examples.
As we know, Indexing starts with 0.
Example 1:
Here, we will create a list that has 10 integers and get the elements based on index numbers provided inside ElementAtOrDefault().
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create List named input_numbers var input_numbers = new List(){100,200,300,456,12,34,56,78,54,44};
//return 6th value Console.WriteLine(«Element present at 6th position: «+input_numbers.ElementAtOrDefault(5));
//return 9th value Console.WriteLine(«Element present at 9th position: «+input_numbers.ElementAtOrDefault(8));
//return 1st value Console.WriteLine(«Element present at 1st position: «+input_numbers.ElementAtOrDefault(0));
//return 4th value Console.WriteLine(«Element present at 4th position: «+input_numbers.ElementAtOrDefault(3));
//return 10th value Console.WriteLine(«Element present at 10th position: «+input_numbers.ElementAtOrDefault(9));
}
}
Output:
Explanation:
1. So first, we created a list named input_numbers that holds 10 integer elements.
2. After that, we searched and displayed the below values using their index positions.
Example 2:
Here, we will create a list that has 3 strings and get the elements based on index numbers provided inside ElementAtOrDefault().
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create List named input_strings var input_strings = new List(){«Linuxhint»,«c#»,«vignan»};
//return 1st value Console.WriteLine(«Element present at 1st position: «+input_strings.ElementAtOrDefault(0));
//return 3rd value Console.WriteLine(«Element present at 3rd position: «+input_strings.ElementAtOrDefault(2));
//return 6th value Console.WriteLine(«Element present at 6th position: «+input_strings.ElementAtOrDefault(5));
//return 9th value Console.WriteLine(«Element present at 9th position: «+input_strings.ElementAtOrDefault(8));
}
}
Output:
Explanation:
1. So first, we created a list named input_strings that holds 3 string elements.
2. After that, we searched and displayed the below strings using their index positions.
The index positions 6 and 9 are not present. Hence, empty is returned.
This is how to return the element based on the index number using ElementAtOrDefault operator available in C# – LINQ. If the element is found at index, the element is returned. If it is not found, it returns empty by default. We demonstrated two different examples to understand the concept better and make sure to use the modules using System, using System.Linq, using System.Collections.Generic in your code.
The LINQ Except() method in C#, it returns all the elements in the first dataset that are not present in the second data set. The data set can be an Array, List, ArrayList, SortedList, and etc.
Syntax:
input_source1.Except(input_source2);
Where input_source1 is the first data source and input_source2 is the second data source.
Example 1:
Here, we will create two Arrays that have string elements and apply the Except() method to return only elements from the first Array that are not present in the second Array.
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Linuxhint { staticvoid Main() { //create array of strings with 5 strings string[] first_strings ={«Linuxhint»,«java»,«python»,«backbone.js»,«ember.js»}; //create array of strings with 3 strings string[] second_strings ={«Linuxhint»,«java»,«html»};
Console.WriteLine(«——–First Array——–«); foreach (var values1 in first_strings) { Console.WriteLine(values1); } Console.WriteLine(«——–Second Array——–«); foreach (var values1 in second_strings) { Console.WriteLine(values1); }
//apply Except() var final=first_strings.Except(second_strings);
1. So first, we created two String Arrays named first_strings,second_strings.
2. After that, we are displaying the flagrante values present in the two arrays using a foreach loop.
3. Use the Except() method and display the values using the foreach loop.
Example 2:
Here, we will create two Arrays that have integer elements and apply the Except() method to return only values from the first Array that are not present in the second Array.
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Linuxhint { staticvoid Main() { //create array of integers int[] first_integers ={20,34,56,23,67,100}; //create array of integers int[] second_integers ={20,23,34,56,67};
Console.WriteLine(«——–First Array——–«); foreach (var values1 in first_integers) { Console.WriteLine(values1); } Console.WriteLine(«——–Second Array——–«); foreach (var values1 in second_integers) { Console.WriteLine(values1); }
//apply Except() var final=first_integers.Except(second_integers);
1. So first, we created two Integer Arrays named first_integers and second_integers.
2. After that, we are displaying the flagrante values present in the two arrays using a foreach loop.
3. Use the Except() method and display the values using the foreach loop.
Conclusion
The LINQ Except() method in C# returns all the elements in the first dataset that are not present in the second data set. Here, we used Array as a data source. Make sure you have to include using System, using System.Linq, using System.Collections, and using System.Collections.Generic.
from iterator in input_source group iterator by iterator.attribute
Where, input_source is the data source(List) and iterator is used to iterate the elements present in input_source.
Example 1:
We will create a List that stores Food details and Group the values in the list based on different attributes.
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint { //define the data for Food class Food { public int food_price { get; set;} public string name { get; set;} public int quantity { get; set;} public string city { get; set;} } static public void Main(){
//group the values in city attribute var result = first_list.GroupBy(element =>element.city);
//display the grouped results using foreach loop foreach (var i in result) { foreach (var j in i) { Console.WriteLine(j.food_price+«->»+j.name+«->»+j.quantity+«->»+j.city); } }
//group the values in name attribute var result2 = first_list.GroupBy(element => element.name);
//display the grouped results using foreach loop foreach (var i in result2) { foreach (var j in i) { Console.WriteLine(j.food_price+«->»+j.name+«->»+j.quantity+«->»+j.city); } }
} }
Output:
Explanation:
1. So first, we created a list that had food details.
2. After that, we are grouping values in the city column. Finally, we are using for each loop to display the grouped values.
3. Lastly, we are grouping values in the name column then we are using for each loop to display the grouped values.
Example 2:
We will create a List that stores Food details and Group the values in the list based on different attributes using Query Syntax.
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint { //define the data for Food class Food { public int food_price { get; set;} public string name { get; set;} public int quantity { get; set;} public string city { get; set;} } static public void Main(){
//group the values in city attribute var result = from iterator1 in first_list group iterator1 by iterator1.city;
//display the grouped results using foreach loop foreach (var i in result) { foreach (var j in i) { Console.WriteLine(j.food_price+«->»+j.name+«->»+j.quantity+«->»+j.city); } }
//group the values in name attribute var result2 = from iterator2 in first_list group iterator2 by iterator2.name;
//display the grouped results using foreach loop foreach (var i in result2) { foreach (var j in i) { Console.WriteLine(j.food_price+«->»+j.name+«->»+j.quantity+«->»+j.city); } }
} }
Output:
Explanation:
1. First, we created a list that had food details.
2. After that, we are grouping values in the city column then we are using for each loop to display the grouped values.
3. Lastly, we are grouping values in the name column then we are using for each loop to display the grouped values.
Conclusion
The LINQ GroupBy method is used to group the similar values in a specified attribute and place all the similar elements using Method and Query. It group all the similar values at one place and we can return the grouped values using foreach loop.
In this article, we will discuss how to return the last element or default element using the LINQ LastOrDefault() function.
Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Regular data structures.
LINQ LastOrDefault()
LastOrDefault() in LINQ returns the last element from the specified data structure. If there are no elements in the data structure, it will return a default value – 0.
There are two ways to use this method. Let’s look into it.
Approach 1: Using Method
We will use the ordinario LastOrDefault() method to return the last/default element.
Syntax:
Where list is the List object created.
Approach 2: Using Query
We will use the Query similar to SQL expression that returns the last/default element.
Syntax:
from i in list select i.LastOrDefault()
Where list is the List object created.
Example 1:
Here, we will create a list that holds integer elements and we will use LastOrDefault() to return only the last value.
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create list of integers Listfirst_list = new List(){100,200,300,456};
Console.WriteLine(«List: «); foreach (var values in first_list) { Console.WriteLine(values); }
//get the last element from the list var result = first_list.LastOrDefault();
Console.WriteLine(«Last element in the List: «+result);
} }
Output:
Explanation:
1. So first, we created a list data structure with 4 integers.
2. After that, we applied LastOrDefault() to get the last element and display the result.
Example 2:
Here, we will create a list that holds no elements and we will use LastOrDefault() to return the default value.
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create list Listfirst_list = new List();
//get the default element from the list var result = first_list.LastOrDefault();
Console.WriteLine(«Default element in the List: «+result);
} }
Output:
Explanation:
1. So first, we created a list data structure with no values.
2. After that, we applied LastOrDefault() to get the default element and display it using the Console.Write() function.
Example 3:
Here, we will create two lists that hold integer and string elements separately. Get the last element from both the lists using Query.
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create list of integers Listfirst_list = new List(){100,200,300,456};
//create list of strings Listsecond_list = new List(){«linuxhint»,«sravan»,«vignan»,«java»};
//get the last element from the list var result2 =(from i in second_list select i).LastOrDefault();
Console.WriteLine(«Last element in the List: «+result2);
} }
Output:
Explanation:
1. Let’s create two Lists:
2. Now, return Last element from both the lists:
Conclusion
LastOrDefault() in LINQ returns only the last element or default element from the data structure. Here, we used List as a data structure. If the data structure has no elements, a default value 0 is returned. We implemented the LINQ LastOrDefault() method in two ways. Make sure that use has to include – using System.Linq und using System.Collections.Generic command lines in your code.
In this article, we will discuss how to return the minimum value through the LINQ Min() function in List data structure.
It is also possible to use other data structures like Stack,Queue and etc.
Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Corriente data structures.
LINQ Min()
Min() in LINQ is used to return the minimum value among the elements.
Syntax: If the values are single:
list.Min()
If there are multiple values:
list.Min(element => element.variable)
Element iterates the values in a list and a variable is one of the values present in a list.
Let’s explore this method.
Example 1:
Here, we will create a list that holds integer elements and we will use Min() to return the minimum value among these elements.
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create data Listfirst_list = new List(){120,80,45,123,456,45,120,8,9,0};
Console.WriteLine(«List: «); foreach (var values in first_list) { Console.WriteLine(values); }
//get the Minimum of elements in the list var result = first_list.Min();
Console.WriteLine(«Minimum value from the above list: «+result);
} }
Output:
Explanation:
First, we created a list data structure with 10 integers.
After that, we applied Min() to get the minimum of the elements.
Finally, we can display the result.
Example 2:
Let’s create Food that holds three attributes – food_price, name, and quantity.
Get minimum food_price and quantity attributes separately.
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint { //define the data for Food class Food { public int food_price { get; set;} public string name { get; set;} public int quantity { get; set;} } static public void Main(){
foreach (var value in first_list) { Console.WriteLine(value.food_price+«->»+value.name+«->»+value.quantity); }
Console.WriteLine(«————————–LINQ Min————————–«);
//minimum food_price var min1 = first_list.Min(element =>element.food_price);
Console.WriteLine(«Minimum food_price: «+min1);
//minimum quantity var min2 = first_list.Min(element =>element.quantity);
Console.WriteLine(«Minimum quantity: «+min2);
//minimum name var min3 = first_list.Min(element => element.name);
Console.WriteLine(«Minimum name: «+min3);
} }
Output:
Explanation:
First we have to declare the structure:
We defined three attributes with food_price and quantity as integer type and name as string type.
Next, we created a list named first_list from the structure-Food.
Add the values to the above created list.
We have added 5 values.
Apply Min() to return the minimum of values from all the attributes.
Conclusion
In this C# – LINQ tutorial, we saw how to return the minimum of the data with the Min() function. It is also possible to return the minimum values in particular attributes in the given data. We discussed two different examples to understand the concept better.
https://codecubit.com/wp-content/uploads/2022/08/word-image-206280-1.png227345RoboLinuxhttps://codecubit.com/wp-content/uploads/2022/05/logo340x156.svgRoboLinux2022-08-11 18:38:492022-08-11 18:39:58LINQ Min() Function
Suppose In a Datasource there are elements with different data types like string,Integer, double etc., and you need to get only a particular type, you should know about OfType() method available in C#. The data source can be an ArrayList.
OfType()
OfType() method in LINQ is used to eliminate the unnecessary data type elements and return only elements of a single data type.
Syntax:
input_source.OfType<datatype>()
Where:
input_source can be an Arraylist.
datatype is the type we will return like string,int,double etc.
Example 1:
Here, we will create a data source named Array List and it has three data type elements. So, we will extract the elements only of string type.
The syntax should be:
input_source.OfType();
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Linuxhint {
public staticvoid Main() { //create an array list that has multiple datatype elements. var my_arraylist=new ArrayList(){1,«Linuxhint»,«java»,4.56,90.5355,6};
//display the ArrayList foreach (var result in my_arraylist){ Console.WriteLine(result); } Console.WriteLine(«————————«);
//return only string type elements var string_types=my_arraylist.OfType();
//display foreach (var result in string_types){ Console.WriteLine(result); } } }
Output:
Explanation:
Create an array list named – my_arraylist.
Return only strings.
Display the result using a foreach loop.
Example 2:
Here, we will create a data source named Array List and it has three data type elements. We will extract the elements only of integer type.
The syntax should be:
input_source.OfType();
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Linuxhint {
public staticvoid Main() { //create an array list that has multiple datatype elements. var my_arraylist=new ArrayList(){1,«Linuxhint»,«java»,4.56,90.5355,6};
//display the ArrayList foreach (var result in my_arraylist){ Console.WriteLine(result); } Console.WriteLine(«————————«);
//return only integer type elements var int_types=my_arraylist.OfType();
//display foreach (var result in int_types){ Console.WriteLine(result); } } }
Output:
Explanation:
Create an array list named – my_arraylist.
Return only integers.
Display the result using a foreach loop.
Example 3:
Here, we will create a data source named Array List and it has three data type elements. We will extract the elements only of double type.
The syntax should be:
input_source.OfType();
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Linuxhint {
public staticvoid Main() { //create an array list that has multiple datatype elements. var my_arraylist=new ArrayList(){1,«Linuxhint»,«java»,4.56,90.5355,6};
//display the ArrayList foreach (var result in my_arraylist){ Console.WriteLine(result); } Console.WriteLine(«————————«);
//return only double type elements var double_types=my_arraylist.OfType();
//display foreach (var result in double_types){ Console.WriteLine(result); } } }
Output:
Explanation:
Create an array list named – my_arraylist.
Return only double type values.
Display the result using a foreach loop.
Conclusion
In this tutorial, we discussed the OfType() method. OfType() method in LINQ is used to eliminate the unnecessary data type elements and return only elements of a single data type. In projects, if you need only particular data types like strings, integers or double values, you can specify int to return only integer values, string to return string values and double to return double values.
In this article, we will discuss how to order the data in descending order using the OrderByDescending() method through LINQ.
Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Regular data structures. It is used to perform queries similar to SQL like expressions.
LINQ OrderByDescending()
OrderByDescending() in LINQ is used to return all elements in a descending order within a given data structure.
OrderByDescending() is also applied on the data that has multiple values in each row. It is possible to order the data based on a particular value in each row.
Syntax:
If the values are single:
list.OrderByDescending(element => element)
element iterates the values in a list and arrange them in Descending order.
element iterates the values in a list and arranges them in descending order and the variable is the value in which the values are arranged in Descending order based on this variable.
Let’s explore this method.
Example 1:
Here, we will create a list that holds integer elements and we will use OrderByDescending() to return these elements in an order.
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create data Listfirst_list = new List(){120,80,45,123,456,45,120,8,9,0};
Console.WriteLine(«List: «); foreach (var values in first_list) { Console.WriteLine(values); }
//order the elements in descending order var ordered_data = first_list.OrderByDescending(element => element);
Console.WriteLine(«Ordered data: «);
//return one by one value from the ordered_data foreach (var result in ordered_data) { Console.WriteLine(result); }
} }
Output:
Explanation:
1. We created a list data structure with 10 integers.
2. After that, we applied OrderByDescending() to order that list by iterating the list using iterator-element.
3. Finally, we can display the result by iterating the ordered_data using a foreach loop.
Example 2:
Here, we will create a list that holds string elements and we will use OrderByDescending() to return these elements in descending order.
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create data Listfirst_list = new List(){«Linuxhint»,«sravan»,«kumar»,«A»};
Console.WriteLine(«List: «); foreach (var values in first_list) { Console.WriteLine(values); }
//order the elements in descending order var ordered_data = first_list.OrderByDescending(element => element);
Console.WriteLine(«Ordered data: «);
//return one by one value from the ordered_data foreach (var result in ordered_data) { Console.WriteLine(result); }
} }
Output:
Explanation:
1. First, we created a list data structure with 4 strings.
2. After that, we applied OrderBy() to order that list by iterating the list using iterator-element.
3. Finally, we can display the result by iterating the ordered_data using a foreach loop.
Example 3:
Let’s create Food that holds three attributes – food_price, name, and quantity.
Order the values based on food_price.
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint { //define the data for Food class Food { public int food_price { get; set;} public string name { get; set;} public int quantity { get; set;} } static public void Main(){
foreach (var value in first_list) { Console.WriteLine(value.food_price+«->»+value.name+«->»+value.quantity); }
Console.WriteLine(«————————–Ordered data (Descending)————————–«);
//order the data based on food_price values in Descending order var ordered_data = first_list.OrderByDescending(element =>element.food_price); foreach (var result in ordered_data) { Console.WriteLine(result.food_price+«->»+result.name+«->»+result.quantity); }
} }
Output:
Explanation:
1. First, we have to declare the structure:
We defined three attributes with food_price and quantity as integer type and name as string type.
2. Next, we created a list named first_list from the structure-Food.
3. Add the values to the above created list.
We have added 5 values.
4. Apply OrderBy() to order the values based on food_price column.
5. Display the result with a foreach loop.
The entire list is ordered in descending order based on values in food_price.
Conclusion
In this C# – LINQ tutorial, we saw how to order the data in descending order with the OrderByDescending() function. It will return all elements in descending order within a given data structure. It is also possible to order the data based on a particular value in each row. We discussed three different examples to understand the concept better.
The LINQ Skip operator is used to return the remaining values by skipping the values from the given data structure. It takes an integer value as a parameter that represents the total number of elements to be skipped from the given data structure.
Syntax:
Or
from element in input_source select element.Skip(n)
Where input_source is the data source.
Parameters:
It takes an integer value(n) that skips that number of elements from the given data structure from first and returns the remaining elements.
Now, consider a scenario where the value of n(integer) is greater than the total number of elements in the data structure, all the elements in the data structure are skipped without any error.
Example 1:
Here, we will create a list that has 10 integers and skip 5 elements using the Skip operator using both the methods(Method and Query).
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create List named input var input = new List(){34,56,78,12,34,53,56,78,90,100};
//return remaining integers by skipping 5 values var result=input.Skip(5); Console.WriteLine(«—Using Method—«); //display the result foreach (int i in result) { Console.WriteLine(i); }
Console.WriteLine(«—Using Query—«);
//return remaining integers by skipping 5 values foreach (int j in (from element in input select element).Skip(5)) { Console.WriteLine(j); }
}
}
Output:
Explanation:
1. First, we created a list named input_numbers that hold 10 integer elements.
2. After that, we are skipping 5 values using Skip() with Method syntax.
3. Finally, we are displaying the remaining values using a foreach loop.
4. Similarly, we are displaying remaining values by skipping 5 values using Query Syntax.
Example 2:
Here, we will create a string array that has 4 strings and skip 6 elements using the Skip operator using both the methods(Method and Query).
using System;
using System.Linq;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create string array named input string[] input ={«Linuxhint»,«java»,«html»,«sravan»};
//skip all strings var result=input.Skip(6); Console.WriteLine(«—Using Method—«); //display the result foreach (string i in result) { Console.WriteLine(i); }
Console.WriteLine(«—Using Query—«);
//display the result by skipping all strings. foreach (string j in (from element in input select element).Skip(6)) { Console.WriteLine(j); }
}
}
Output:
So, you can see that all the elements are skipped.
Explanation:
Create a string array named input that holds 4 strings.
Use Method syntax to skip all values using the Skip() operator.
Use Query syntax to skip all values using the Skip() operator.
Conclusion
We have seen how to skip the elements using Skip() operator in C# – LINQ. It takes an integer value(n) that skips that number of elements from the given data structure from first and returns the remaining elements.
In each example we implemented Skip() with Method as well as Query syntax. Finally, we came to notice that if the value of n(integer) is greater than the total number of elements in the data structure, then all the elements in the data structure are skipped without any error.
Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Común data structures. With LINQ, we can also perform database query operations. LINQ supports many methods and functions that are used for data manipulation, like updating, deletion, insertion, etc.
LINQ Take()
The LINQ Take operator is used to return the values from the given data structure. It takes an integer value as a parameter that represents the total number of elements to be retrieved from the given data structure.
Syntax:
Or
from element in input_source select element.Take(n)
Where input_source is the data source.
Parameters:
It takes an integer value(n) which is used to return that number of elements from the given data structure.
Now consider a scenario where the value of n(integer) is greater than the total number of elements in the data structure. All the elements in the data structure are returned without any error.
Example 1:
Here, we will create a list that has 10 integers and get 5 elements using the Take operator using both the methods(Method and Query).
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create List named input var input = new List(){34,56,78,12,34,53,56,78,90,100};
//return 5 integers var result=input.Take(5); Console.WriteLine(«—Using Method—«); //display the result foreach (int i in result) { Console.WriteLine(i); }
Console.WriteLine(«—Using Query—«);
//display the result by returning 5 integers. foreach (int j in (from element in input select element).Take(5)) { Console.WriteLine(j); }
}
}
Output:
Explanation:
1. So firstly, we created a list named input_numbers that holds 10 integer elements.
2. After that we are taking only 5 values using Take() with Method syntax.
3. Finally, we are displaying the result using a foreach loop.
Example 2:
Here, we will create a string array that has 4 strings and get 6 elements using the Take operator using both the methods(Method and Query).
using System;
using System.Linq;
//create a class – Linuxhint class Linuxhint {
static public void Main(){
//create string array named input string[] input ={«Linuxhint»,«java»,«html»,«sravan»};
//return all strings var result=input.Take(6); Console.WriteLine(«—Using Method—«); //display the result foreach (string i in result) { Console.WriteLine(i); }
Console.WriteLine(«—Using Query—«);
//display the result by returning all strings. foreach (string j in (from element in input select element).Take(6)) { Console.WriteLine(j); }
}
}
Output:
You can see that all the elements from the string array were returned without any error.
Explanation:
Create a string array naemd input that holds 4 strings.
Use Method syntax to get all values using the Take() operator.
Use Query syntax to get all values using the Take() operator.
Conclusion
So we have seen how to return the elements using Take() operator in C# – LINQ. It takes an integer value as a parameter that represents the total number of elements to be retired from the given data structure. In each example, we implemented Take() with Method as well as Query syntax. Finally, we came to notice that if the value of an integer is greater than the total number of elements in the data structure, then all the elements in the data structure are returned without any error.
In this article, we will discuss how to order the data based on multiple attributes in descending order using the ThenBy() Method() method through LINQ.
Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Ordinario data structures. It is used to perform queries similar to SQL Like expressions.
LINQ ThenBy() Method
ThenBy() Method in LINQ is used to return all elements in an ascending order within a given data structure based on multiple attributes. So we have to use the ThenBy() Method along with the OrderBy()/OrderByDescending() methods.
First we will apply the OrderBy()/OrderByDescending() method and ThenBy() is used.
Syntax:
If the values are single:
ThenBy(element => element)
The element iterates the values in a list and arranges them in ascending order.
If there are multiple values:
ThenBy(element => element.variable)
The element iterates the values in a list and arranges them in ascending order, and the variable is the value by which the values are arranged in ascending order based on this variable.
Whereas, a list is the input list that holds values and a variable refers to an attribute name in which we will order based on this variable only.
Example 1: OrderBy() with ThenBy()
Let’s create Food that holds three attributes – food_price, name, and quantity.
Order the values based on food_price with OrderBy() and food_name with ThenBy().
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint { //define the data for Food class Food { public int food_price { get; set;} public string name { get; set;} public int quantity { get; set;} } static public void Main(){
//create data List first_list = new List(); //add values first_list.Add(new Food { food_price=300,name=«parota»,quantity=1}); first_list.Add(new Food { food_price=800,name=«paneer»,quantity=4}); first_list.Add(new Food { food_price=100,name=«mushroom»,quantity=2}); first_list.Add(new Food { food_price=100,name=«chips»,quantity=10}); first_list.Add(new Food { food_price=400,name=«fruits»,quantity=8});
foreach (var value in first_list) { Console.WriteLine(value.food_price+«->»+value.name+«->»+value.quantity); }
//order the data based on food_price values in ascending and name in ascending var ordered_data = first_list.OrderBy(element => element.food_price).ThenBy(element => element.name); foreach (var result in ordered_data) { Console.WriteLine(result.food_price+«->»+result.name+«->»+result.quantity); }
} }
Output:
Explanation:
1. First we have to declare the structure:
We defined three attributes with food_price and quantity as an integer type and name as a string type.
2. Next we created a list named first_list from the structure-Food.
3. Add the values to the above-created list.
We have added 5 values.
4. Apply OrderBy() to order the values based on the food_price column in ascending order and ThenBy) method to order the values in the name column in ascending order.
5. Display the result with a foreach loop.
So the entire list is ordered in ascending order based on values in food_price and in ascending order based on values in the name attribute.
Example 2: OrderByDescending() with ThenBy()
Let’s create Food that holds three attributes: food_price, name, and quantity.
Order the values based on food_price with OrderByDescending() and food_name with ThenBy().
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint { //define the data for Food class Food { public int food_price { get; set;} public string name { get; set;} public int quantity { get; set;} } static public void Main(){
//create data List first_list = new List(); //add values first_list.Add(new Food { food_price=300,name=«parota»,quantity=1}); first_list.Add(new Food { food_price=800,name=«paneer»,quantity=4}); first_list.Add(new Food { food_price=100,name=«mushroom»,quantity=2}); first_list.Add(new Food { food_price=100,name=«chips»,quantity=10}); first_list.Add(new Food { food_price=400,name=«fruits»,quantity=8});
foreach (var value in first_list) { Console.WriteLine(value.food_price+«->»+value.name+«->»+value.quantity); }
//order the data based on food_price values in descending and name in ascending order. var ordered_data = first_list.OrderByDescending(element => element.food_price).ThenBy(element => element.name); foreach (var result in ordered_data) { Console.WriteLine(result.food_price+«->»+result.name+«->»+result.quantity); }
} }
Output:
Explanation:
1. First we have to declare the structure:
So, we defined three attributes, with food_price and quantity as an integer type and name as a string type.
2. Next we created a list named first_list from the structure-Food.
3. Add the values to the above created list.
We have added 5 values.
4. Apply OrderByDescending() to order the values based on the food_price column in descending order and ThenBy() method to order the values in the name column in ascending order.
5. Display the result with a foreach loop.
So the entire list is ordered in descending order based on values in food_price and in ascending order based on values in the name attribute.
Example 3: OrderBy() with multiple ThenBy()
Let’s create Food that holds three attributes: food_price, name, and quantity.
Order the values based on food_price with OrderByDescending() and food_name,quantity with ThenBy().
using System;
using System.Linq;
using System.Collections.Generic;
//create a class – Linuxhint class Linuxhint { //define the data for Food class Food { public int food_price { get; set;} public string name { get; set;} public int quantity { get; set;} } static public void Main(){
//create data List first_list = new List(); //add values first_list.Add(new Food { food_price=300,name=«parota»,quantity=1}); first_list.Add(new Food { food_price=800,name=«paneer»,quantity=4}); first_list.Add(new Food { food_price=100,name=«mushroom»,quantity=2}); first_list.Add(new Food { food_price=100,name=«chips»,quantity=10}); first_list.Add(new Food { food_price=400,name=«fruits»,quantity=8});
foreach (var value in first_list) { Console.WriteLine(value.food_price+«->»+value.name+«->»+value.quantity); }
//order the data based on food_price values in descending and name,quantity in ascending order. var ordered_data = first_list.OrderByDescending(element => element.food_price). ThenBy(element => element.name). ThenBy(element => element.quantity); foreach (var result in ordered_data) { Console.WriteLine(result.food_price+«->»+result.name+«->»+result.quantity); }
} }
Output:
Explanation:
1. First we have to declare the structure:
So, we defined three attributes with food_price and quantity as an integer type and name as a string type.
2. Next we created a list named first_list from the structure-Food.
3. Add the values to the above-created list.
We have added 5 values.
4. Apply OrderByDescending() to order the values based on the food_price column in descending order and the ThenBy() method to order the values in the name and quantity columns in ascending order.
5. Display the result with a foreach loop.
So, the entire list is ordered in descending order based on values in food_price and in ascending order based on values in name and quantity attributes.
Conclusion
In the LINQ tutorial, we saw how to order the data by multiple attributes with the ThenBy() method and OrderBy()/OrderByDescending() functions. It is also possible to order the data based on a particular value by providing multiple attributes. We discussed three different examples to understand the concept better. Make sure that you import the using System, System.Linq, und System.Collections.Generic;