C# LINQ Last() Method
Language Integrated Query language (LINQ) is used to perform operations on the C# collections or normal data structures.
LINQ Last()
The Last() method in LINQ returns the last element from the specified data structure. There are two ways to use this method. Let’s look into it.
Approach 1: Using Method
We will use the normal Last() method to return the last element.
Syntax:
Where the list is the List object created.
Approach 2: Using Query
Syntax:
Where the list is the List object created.
Example 1:
Here, we will create a list that holds the integer elements and use the Last() method to return only the last value.
using System.Linq;
using System.Collections.Generic;
//create a class - Linuxhint
class Linuxhint
{
static public void Main(){
//create list of integers
List<int> first_list = new List<int>() {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.Last();
Console.WriteLine("Last element in the List: "+result);
}
}
Output:
Explanation:
1. First, we created a list data structure with 4 integers.
2. After that, we applied the Last() method to get the last element and display the result.
Example 2:
Here, we will create a list that holds the string elements and use the Last() method to return only the last element.
using System.Linq;
using System.Collections.Generic;
//create a class - Linuxhint
class Linuxhint
{
static public void Main(){
//create list of strings
List<string> first_list = new List<string>() {"linuxhint","sravan","vignan","java"};
Console.WriteLine("List: ");
foreach (var values in first_list)
{
Console.WriteLine(values);
}
//get the last element from the list
var result = first_list.Last();
Console.WriteLine("Last element in the List: "+result);
}
}
Output:
Explanation:
- First, we created a list data structure with 4 strings.
- After that, we applied the Last() method to get the last element and display it using the Console.Write() function.
Example 3:
Here, we will create two lists that hold the integer and string elements separately. Get the last element from both lists using Query.
using System.Linq;
using System.Collections.Generic;
//create a class - Linuxhint
class Linuxhint
{
static public void Main(){
//create list of integers
List<int> first_list = new List<int>() {100,200,300,456};
//create list of strings
List<string> second_list = new List<string>() {"linuxhint","sravan","vignan","java"};
Console.WriteLine("First List: ");
foreach (var values in first_list)
{
Console.WriteLine(values);
}
//get the Last element from the list
var result = (from i in first_list select i).Last();
Console.WriteLine("Last element in the List: "+result);
Console.WriteLine("Second List: ");
foreach (var values2 in second_list)
{
Console.WriteLine(values2);
}
//get the last element from the list
var result2 = (from i in second_list select i).Last();
Console.WriteLine("Last element in the List: "+result2);
}
}
Output:
Explanation:
- We created two lists.
- We returned the last element from both lists.
Conclusion
We came to the end of this chapter. The Last() method in LINQ returns only the last element from the data structure. Here, we used the List as a data structure. We implemented the LINQ Last() method in two ways. Make sure that use has to include using System.Linq and using System.Collections.Generic command lines in your code.
Source: linuxhint.com