Solved

How can I use DISTINCT in LINQ?

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
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 …

770 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