Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. How to get a distinct records with multiple columns using linq

How to get a distinct records with multiple columns using linq

Scheduled Pinned Locked Moved LINQ
questioncsharplinqtutorial
4 Posts 4 Posters 31 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 2112122
    wrote on last edited by
    #1

    How to get a distinct records with multiple columns using linq? Following code is returning multiple records, which is correct, but I want to get distinct records from the table p ( for p.PId)? How do i do that? listViewModel.EmailRecipients.AddRange( from r join p on r.PId equals p.PId join gp on p.PId equals gp.PId join hp on gp.HPId equals hp.HPId orderby p.LastName, p.FirstName select(new EmailRecipientViewModel { PId = p.PId, FirstName = p.FirstName, LastName = p.LastName, Email = "", }) );

    L H H 3 Replies Last reply
    0
    • M Member 2112122

      How to get a distinct records with multiple columns using linq? Following code is returning multiple records, which is correct, but I want to get distinct records from the table p ( for p.PId)? How do i do that? listViewModel.EmailRecipients.AddRange( from r join p on r.PId equals p.PId join gp on p.PId equals gp.PId join hp on gp.HPId equals hp.HPId orderby p.LastName, p.FirstName select(new EmailRecipientViewModel { PId = p.PId, FirstName = p.FirstName, LastName = p.LastName, Email = "", }) );

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Your question implies you have "duplicate" (employee?) records. Instead if trying to retrieve "distinct" records, you should figure out why you have duplicates in the first place. Otherwise, just "group" on the "field" you want to be "distinct" (if that makes any sense).

      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

      1 Reply Last reply
      0
      • M Member 2112122

        How to get a distinct records with multiple columns using linq? Following code is returning multiple records, which is correct, but I want to get distinct records from the table p ( for p.PId)? How do i do that? listViewModel.EmailRecipients.AddRange( from r join p on r.PId equals p.PId join gp on p.PId equals gp.PId join hp on gp.HPId equals hp.HPId orderby p.LastName, p.FirstName select(new EmailRecipientViewModel { PId = p.PId, FirstName = p.FirstName, LastName = p.LastName, Email = "", }) );

        H Offline
        H Offline
        Harsh Shah ifour
        wrote on last edited by
        #3

        You just have to add Distinct() keyword at the end of the select statement as i added below. Try this:

        listViewModel.EmailRecipients.AddRange(
        from r
        join p on r.PId equals p.PId
        join gp on p.PId equals gp.PId
        join hp on gp.HPId equals hp.HPId

        orderby p.LastName, p.FirstName
        select(new EmailRecipientViewModel)
        {
        PId = p.PId,
        FirstName = p.FirstName,
        LastName = p.LastName,
        Email = "",
        }).Distinct();

        1 Reply Last reply
        0
        • M Member 2112122

          How to get a distinct records with multiple columns using linq? Following code is returning multiple records, which is correct, but I want to get distinct records from the table p ( for p.PId)? How do i do that? listViewModel.EmailRecipients.AddRange( from r join p on r.PId equals p.PId join gp on p.PId equals gp.PId join hp on gp.HPId equals hp.HPId orderby p.LastName, p.FirstName select(new EmailRecipientViewModel { PId = p.PId, FirstName = p.FirstName, LastName = p.LastName, Email = "", }) );

          H Offline
          H Offline
          Hailu Worku Obsse
          wrote on last edited by
          #4

          You simply add first to the end of the Select logic listViewModel.EmailRecipients.AddRange( from r join p on r.PId equals p.PId join gp on p.PId equals gp.PId join hp on gp.HPId equals hp.HPId orderby p.LastName, p.FirstName select(new EmailRecipientViewModel { PId = p.PId, FirstName = p.FirstName, LastName = p.LastName, Email = "", }) ).First();

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups