Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4029196
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:18:53+00:00 2026-05-20T11:18:53+00:00

If I have a query/results set as follows… from t1 in table1 join t2

  • 0

If I have a query/results set as follows…

from t1 in table1
join t2 in table2 on t1.ID equals t2.ID
join t3 in table3 on t2.ID equals t3.ID
select new MyClass()
{
   ID = t1.ID,
   Name = t2.Name,
   Rank = t2.Rank,
   City = t3.City
}

ID | Name | City | Rank
01 | Test | Fake | 876
01 | Test | Fake | 755
02 | Blah | Fake | 765

Doing a .Distinct() will return all 3 records, but what if I want the first and third record and don’t want to eliminate any columns from my results set? Is there a way to specify columns to ignore when doing a distinct, or explicitly include in a distinct so that I could do something like this…

// pseudo code
.Distinct(o => o.Name.FirstOrDefault())

I think it’s possible to do this using group by, but that seems like it would be slow and messy, especially if I needed to group on multiple columns in order to not exclude too many rows. Any ideas? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:18:54+00:00Added an answer on May 20, 2026 at 11:18 am

    No, you can’t specify a projection by which to determine equality for Distinct. (I have extension methods to do this for LINQ to Objects, but that’s a different matter.)

    I suggest you use GroupBy, possibly in conjunction with First:

    var query = from t1 in table1
                join t2 in table2 on t1.ID equals t2.ID
                join t3 in table3 on t2.ID equals t3.ID
                select new { t1.ID, t2.Name, t2.Rank, t3.City } into tuple
                group tuple by new { tuple.ID, tuple.Name, tuple.City } into grouping
                let first = grouping.First() // For convenience
                select new MyClass
                {
                    ID = first.ID, City = first.City,
                    Name = first.Name, Rank = first.Rank
                };
    

    Note that the results of this may not be deterministic – you’re taking the first item of each group, ignoring the rank – so you may get the record with rank 876 or you may get the one with rank 755 in your example.

    I don’t know exactly what this will look like in SQL – but you should check via logging, and then run the query profiler to see what the cost will be like.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Doctrine query as follows... $qb = $this->createQueryBuilder('s') ->select('s, csr, c') ->Join('s.classesreference',
Hi all I have a sql query SELECT FKid1, FKID2, a, b, c from
I have two queries serving the same purpose. SELECT * FROM #user INNER JOIN
I have a query which returns the result set as follows: Col1 A B
I have a page that has to render a huge set of query results
I have a query that creates a result set like this: Rank Name 1
I have a query that returns a result set similar to the one below:
I have this query which returns a result set with Date(column) as nvarchar datatype.
I have a dynamic query which I call and I put the Result set
I have a java.sql.ResultSet object containg data from a query that was run. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.