| by Arround The Web | No comments

LINQ Min() Function

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 Normal 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(){

       //create data
       Listfirst_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=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 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.

Share Button

Source: linuxhint.com

Leave a Reply