| by Arround The Web | No comments

LINQ Count() Function

In this article, we will discuss how to return the total number of values through the LINQ Count() function in List data structure.

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

LINQ Count()

Count() in LINQ is used to return the total count (number) of values present in a data structure (List).

Syntax:

If the values are single/Multiple:

list.Count()

element iterates the values in a list.

We can also use SQL Expression/Query to return count of values.

Syntax:

In particular Attribute – from i in list select i.attribute.Count()

From Entire List – from i in list select i.Count()

Where:

i is the iterator and attribute() is used to get count of particular attribute from the list.

Let’s explore these methods.

Example 1:

Here, we will create a list that holds integer elements and we will use Count() to return the total number of values.

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 count of elements in the list
       var result = first_list.Count();

Console.WriteLine("Number of Integers in the above list: "+result);

}
}

Output:

Explanation:

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

After that, we applied Count() to get the total number 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 the count of elements in the list.

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 Count--------------------------");

         //count
       var result = first_list.Count();  

Console.WriteLine("Count: "+result);

}
}

Output:

Explanation:

First, we have to declare the structure:

So, 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 Count() to return the total number of values.

Example 3:

Here, we will use SQL Expression to get the count of values from food_price and from the entire list.

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=34,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 Count--------------------------");

         //count the values present in food_price attribute
       var result = (from i in first_list select i.food_price).Count();  

Console.WriteLine("food_price Count: "+result);

       //entire list count
       var result2 = (from i in first_list select i).Count();  

Console.WriteLine("Entire Count: "+result2);

}
}

Output:

Conclusion

In the LINQ tutorial, we saw how to return the total count (number) of the values present in the list with the Count() function. It is also possible to return the total values in particular attributes/entire list using SQL expression in the given data. We discussed three different examples to understand the concept better.

Share Button

Source: linuxhint.com

Leave a Reply