Solved

How can I use DISTINCT in LINQ?

Posted on 2009-07-02
2
236 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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

791 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