Solved

How can I use DISTINCT in LINQ?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Log4Net custom Appender not being fired. 5 41
SignalR - getting new data from sql DB 5 64
asp.net bundle 8 36
Wpf develop 5 32
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

19 Experts available now in Live!

Get 1:1 Help Now