c sharp

LINQ Max() Function

In this article, we will discuss how to return the maximum value through the LINQ Max() function in List data structure.

Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Normal data structures.

LINQ Max()

Max() in LINQ is used to return the maximum value among the elements present in a data structure.

Syntax:

If the values are single:

list.Max()

If there are multiple values:

list.Max(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 Max() to return the maximum 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
       List<int> first_list = new List<int>() {120,80,45,123,456,45,120,8,9,0 };
       
       
       Console.WriteLine("List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
           
           
        //get the Maximum of elements in the list
       var result = first_list.Max();
           
         Console.WriteLine("Maximum value from the above list: "+result);
         
}
}

Output:

Explanation:

1. So first, we created a list data structure with 10 integers.

2. After that, we applied Max() to get the maximum of the elements.

3. Finally, we can display the result.

Example 2:

Let’s create Food that holds three attributes: food_price, name, and quantity.

Get maximum 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(){
       
       //create data
       List<Food> first_list = new List<Food>();
       //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=564,name="vegtables",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);  
            }  
           
           
           
        Console.WriteLine("------------------------LINQ Max------------------------");

         //maximum food_price
       var max1 = first_list.Max(element => element.food_price);  
             
       Console.WriteLine("Maximum food_price: "+max1);
       
         //maximum quantity
       var max2 = first_list.Max(element => element.quantity);  
             
       Console.WriteLine("Maximum quantity: "+max2);
       
       //maximum name
       var max3 = first_list.Max(element => element.name);  
             
       Console.WriteLine("Maximum name: "+max3);
           
           
}
}

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 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 Max() to return the maximum of values from all the attributes.

Conclusion

In this C# – LINQ tutorial, we saw how to return the maximum of the data with the Max() function. It is also possible to return the maximum values in particular attributes in the given data. We discussed two different examples to understand the concept better.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain