Sumedh Meshram

A Personal Blog


Select distinct from generic list using linq

If you have a generic list which has a multiple data like

ID  FirstName  LastName  City  State Zip

1   ABC           Z              PN   LA     25341

2   XYZ           R              UI   TG     56742

1   ABC           W             PN   LA     25341

3   UHY           R             HJ    UH    34256

3   UHY           J              HJ    UH    34256 

Now if you want to get select distinct rows on based on ID, then through Linq this can be done easily as :


var unique =
    from w in genericList
    group w by w.ID
    into g
        select g.FirstOrDefault();


This will be useful where we have the generic list with duplicate data or data with duplicate IDs.



Add comment

  Country flag

  • Comment
  • Preview