Quantcast
Channel: Geeks Tutor
Viewing all articles
Browse latest Browse all 38

How to get array of distinct values from List using LINQ ?

$
0
0

Below is a sample code snippet demonstrating how to get array of distinct values from List using LINQ in C#.

How to get array of distinct values from List using LINQ ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace GinktageConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee { Name = "Praveen", Experience = 6 });
            employees.Add(new Employee { Name = "Naveen", Experience = 4 });
            employees.Add(new Employee { Name = "Praveen", Experience = 1 });

            var query = (from employee in employees
                         select employee.Name).Distinct().ToArray();
            foreach(var item in query)
            {
                Console.WriteLine("Name : " + item + "\n");
                
            }
            Console.ReadLine();
        }
    }
    public class Employee
    {
        public string Name { get; set; }
        public int Experience { get; set; }
    }
}

The post How to get array of distinct values from List using LINQ ? appeared first on TechBlog.


Viewing all articles
Browse latest Browse all 38

Trending Articles