Solved

How can I use DISTINCT in LINQ?

Posted on 2009-07-02
2
237 Views
Last Modified: 2013-11-11
I have written the attached linq query but I only want to return data where the "ApplicantName" and  "vdv.Title" are DISTINCT.....possible?

Thanks.
var query = from ada in db.ApplicantDetails
                    join vdv in db.VacancyDetails on ada.VacancyID equals vdv.VacancyID
                    join adag in db.AgencyDetails on ada.AgencyID equals adag.AgencyID
                    join aaa in db.AgencyAgents on adag.AgencyID equals aaa.AgencyID
                    join rlv in db.ResultLookups on ada.ResultCode equals rlv.ResultCode
                    join aia in db.ApplicantInterviews on ada.ApplicantID equals aia.ApplicantID into appinterviews
                    from aia in appinterviews.DefaultIfEmpty()
                    orderby ada.LastName
                    select new { ada.ApplicantID, ada.VacancyID, ada.AgencyID, ada.ResultCode, ApplicantName = ada.Salutation + ' ' + ada.FirstName + ',' + ' ' + ada.LastName, ada.Address, ada.Phone, ada.Email, ada.CVFilePath, vdv.Title, ClosingDate = vdv.ClosingDate.ToShortDateString(), adag.Company, rlv.ResultDescription, aaa.ContactFirstName, aaa.ContactLastName, aaa.AgentPhone, Successful = (aia.Successful == null) ? false : (aia.Successful) };
 
        e.Result = query;

Open in new window

0
Comment
Question by:Shepwedd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
Rahul Goel ITIL earned 500 total points
ID: 24761794

IEnumerable<Search> myDistinctSearches = var mySearches = context.Searches
   .Where(s => s.resultCount > 0)
   .OrderByDescending(s => s.dateCreated)
   .Distinct(new MyComparer())
   .Take(5);
 
public class MyComparer : IEqualityComparer<Search>
{
   public bool Equals(Search x, Search y) {
      return x.searchPhrase == y.searchPhrase;
   }
 
   public int GetHashCode(Search obj) {
      return obj.searchPhrase.GetHashCode();
   }
}
 
 
IEnumerable<Search> myDistinctSearches = var mySearches = context.Searches
   .Where(s => s.resultCount > 0)
   .OrderByDescending(s => s.dateCreated)
   .ToList()
 
   .Distinct(new MyComparer())

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24761810
wow! but how would i impliment the distinct through your suggestion?

thanks.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question