LINQ – Average, Count, Max, Sum, First, Contains, ElementAt, Distinct Example

In this tutorial, you will learn:
  1. What are Average, Count, Max, Min, Sum, First, Last, Contains, ElementAt and Distinct method in LINQ
  2. Programming Example
  1. Average() - Average Method calculates the average value of numeric data.
  2. Count() – Count method count the present items in list.
  3. Max() – It picks the maximum numeric values from the list.
  4. Min() – It picks the minimum numeric values from the list.
  5. Sum() – It calculates the sum of total numeric value present in the list.
  6. First() – It picks the first value present in the list.
  7. Last() – It picks the last value present in the list.
  8. Contains() – It find the value in the list and returns Boolean (true/false) result.
  9. ElementAt() – It picks the value from the list on the given position.
  10. Distinct() – It removes duplicate value and picks only unique elements.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqTutorial
{
    class ProductStore
    {
        public string productName { get; set; }
        public int productPrice { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Creating List
            IList<ProductStore> productList = new List<ProductStore>();

            productList.Add(new ProductStore { productName = "Hard Disk", productPrice = 1280 });
            productList.Add(new ProductStore { productName = "Monitor", productPrice = 3000 });
            productList.Add(new ProductStore { productName = "SSD Disk", productPrice = 3500 });
            productList.Add(new ProductStore { productName = "RAM", productPrice = 2450 });
            productList.Add(new ProductStore { productName = "Processor", productPrice = 7680 });
            productList.Add(new ProductStore { productName = "Bluetooth", productPrice = 540 });
            productList.Add(new ProductStore { productName = "Keyboard & Mouse", productPrice = 1130 });

            //LINQ Query Syntax
            var result = from p in productList
                         select p.productPrice;

            Console.WriteLine("Average: " + result.Average());
            Console.WriteLine("Count: " + result.Count());
            Console.WriteLine("Max: " + result.Max());
            Console.WriteLine("Min: " + result.Min());
            Console.WriteLine("Sum: " + result.Sum());
            Console.WriteLine("First Value: " + result.First());
            Console.WriteLine("Last Value: " + result.Last());
            Console.WriteLine("Is 3500 Available? " + result.Contains(3500));
            Console.WriteLine("Element at 4th Position: " + result.ElementAt(3));

            var distinctPrice = result.Distinct();
            Console.WriteLine("\n\n------- Distinct Result --------\n");
            foreach (var price in distinctPrice)
            {
                Console.WriteLine("Distinct Value: " + price.ToString());
            }

            //LINQ Method Syntax. Uncomment it to see the result.
            /*
            Console.WriteLine("Average: " + productList.Average(p => p.productPrice));
            Console.WriteLine("Count: " + productList.Count());
            Console.WriteLine("Max: " + productList.Max(p => p.productPrice));
            Console.WriteLine("Min: " + productList.Min(p => p.productPrice));
            Console.WriteLine("Sum: " + productList.Sum(p => p.productPrice));
            */
            Console.ReadKey();
        }
    }
}

You can use Method Syntax or Lambda Expression as follow:

  Console.WriteLine("Average: " + productList.Average(p => p.productPrice));
  Console.WriteLine("Count: " + productList.Count());
  Console.WriteLine("Max: " + productList.Max(p => p.productPrice));
  Console.WriteLine("Min: " + productList.Min(p => p.productPrice));
  Console.WriteLine("Sum: " + productList.Sum(p => p.productPrice));
Output:


Average: 2797.14285714286
Count: 7
Max: 7680
Min: 540
Sum: 19580
First Value: 1280
Last Value: 1130
Is 3500 Available? True
Element at 4th Position: 2450


------- Distinct Result --------

Distinct Value: 1280
Distinct Value: 3000
Distinct Value: 3500
Distinct Value: 2450
Distinct Value: 7680
Distinct Value: 540
Distinct Value: 1130

_

Summary

In this tutorial, you learned Average, Count, Max, Min, Sum, First, Last, Contains, ElementAt and Distinct method with c# example. In the next chapter, you will learn All() Method in Linq.

 

Share your thought