Linq query to get distinct name count in C#

 Using this program we are using Linq query to get distinct name count in C#
Program: 

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
             var employees = new List<Employee> {
                new Employee { Name = "Surya", EId = 1, Salary= 4000 },
                new Employee { Name = "Infoware", EId = 2, Salary = 12.50M },
                new Employee { Name = "Surya", EId = 3, Salary = 1.15M },
                new Employee { Name = "Infoware", EId = 2, Salary = 2.00M },
                new Employee { Name = "SuryaInfoware.com", EId = 5, Salary = 1000.0M }
              };
            //linq query to find distinct employee names in the list of employees
            var distinctEmployeeNames = (from emp in employees
                                         group emp by emp.Name
                                       into myEmploy
                                         select myEmploy.FirstOrDefault()).ToList().Count();

            Console.WriteLine("Distinct employee names {0} ", distinctEmployeeNames.ToString());

        }
    }
    class Employee
    {
         public string Name;
         public int EId;
         public decimal Salary;
    }
}

Output:

Share this post :

Post a comment

Please share your valuable feedback and share this article in social media.

 
Support : Ranga Rajesh Kumar
Copyright © 2019. Error Solution - All Rights Reserved
Site Designed by Ranga Rajesh Kumar