Lambda expression to get distinct records based on two columns in C#

In this program we are going use Lambda expression to get distinct employee list based on employee id and employee name (two) columns. 


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 = "Surya Infoware", EId = 5, Salary = 1000.0M }
              };

            var DistinctEmployNames = employees.Select(p => new { p.EId, p.Name }).Distinct().Count();
            Console.WriteLine("Distinct employee names {0} ", DistinctEmployNames.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