01 | using System; |
02 |
03 | using System.Collections.Generic; |
04 |
05 | using System.Data; |
06 |
07 | using System.Linq; |
08 |
09 | namespace AbundantCode |
10 |
11 | { |
12 |
13 | internal class Program |
14 |
15 | { |
16 |
17 | //How to Remove Duplicates and Get Distinct records from List using LINQ ? |
18 |
19 | private static void Main( string [] args) |
20 |
21 | { |
22 |
23 | List<Employee> employees = new List<Employee>() |
24 |
25 | { |
26 |
27 | new Employee { EmpID = 1 , Name = "Ram" }, |
28 |
29 | new Employee { EmpID = 2 , Name = "Magi" }, |
30 |
31 | new Employee { EmpID = 3 , Name = "Muni" }, |
32 |
33 | new Employee { EmpID = 3 , Name = "Natarajan" } |
34 |
35 | }; |
36 |
37 | //Gets the Distinct List |
38 |
39 | var DistinctItems = employees.GroupBy(x => x.EmpID).Select(y => y.First()); |
40 |
41 | foreach (var item in DistinctItems) |
42 |
43 | Console.WriteLine(item.Name); |
44 |
45 | Console.ReadLine(); |
46 |
47 | } |
48 |
49 | } |
50 |
51 | public class Employee |
52 |
53 | { |
54 |
55 | public string Name { get ; set ; } |
56 |
57 | public int EmpID { get ; set ; } |
58 |
59 | } |
60 |
61 | } |
0 Comments