How to get a distinct records with multiple columns using linq
-
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 = "", }) );
-
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 = "", }) );
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
-
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 = "", }) );
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.HPIdorderby p.LastName, p.FirstName
select(new EmailRecipientViewModel)
{
PId = p.PId,
FirstName = p.FirstName,
LastName = p.LastName,
Email = "",
}).Distinct(); -
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 = "", }) );
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();