Solved

How can I use DISTINCT in LINQ?

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now