Schlagwortarchiv für: LastOrDefault


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»};
   
   
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).LastOrDefault();
   
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).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.



Source link