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

  • Home
  • SEARCH
  • 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 708343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:22:49+00:00 2026-05-14T04:22:49+00:00

This should be and easy one for the LINQ gurus out there. I’m doing

  • 0

This should be and easy one for the LINQ gurus out there.

I’m doing a complex Query using UNIONS and CONTAINSTABLE in my database to return ranked results to my application.

I’m getting duplicates in my returned data. This is expected. I’m using CONTAINSTABLE and CONTAINS to get all the results I need. CONTAINSTABLE is ranked by SQL and CONTAINS (which is run only on the Keywords field ) is hard-code-ranked by me. ( Sorry if that doesn’t make sense )

Anyway, because the tuples aren’t identical ( their rank is different ) a duplicate is returned.

I figure the best way to deal with this is use LINQ.

I know I’ll be using the Distinct() extension method, but do I have to implement the IEqualityComparer interface? I’m a little fuzzy on how to do this.

For argument’s sake, say my resultset is structured like this class:

class Content {
  ContentID int //KEY
  Rank int 
  Description String 
}

If I have a List<Content> how would I write the Distinct() method to exclude Rank? Ideally I’d like to keep the Content’s highest Rank. SO, if one Content’s RAnk is 112 and the other is 76. I’d like to keep the 112 rank.

Hopefully I’ve given enough information.

EDIT

Here’s a sample of the SQL for anyone who has an Idea on how to do this in SQL:

UNION ALL  
SELECT
    p.ProductID     AS ContentID
  , p.ProductName   AS Title
  , K.RANK          AS Rank
  , 'Product'       AS ContentType
  , p.AddedDateTime

FROM Products AS p
 INNER JOIN CONTAINSTABLE(NaturalFactorsPIM.dbo.Products,*, @SearchPred, @TopN) AS K ON K.[KEY] = P.ProductID
WHERE @IncludeProducts = 1 
  AND p.ProductStatus = 1 

UNION ALL  
SELECT
   p.ProductID    AS ContentID
 , p.ProductName  AS Title
 , 80             AS Rank
 , 'Product'      AS ContentType
 , p.AddedDateTime

FROM Products AS p
WHERE FREETEXT( p.ProductKeywords, @SearchPred)
  AND @IncludeProducts = 1 
  AND p.ProductStatus = 1 

ORDER BY Rank DESC
  • 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-14T04:22:50+00:00Added an answer on May 14, 2026 at 4:22 am

    This should do what you want:

    Var List<Content> AllTheData = GetTheDataForThisExample()
    Var DistinctDataWithMaxRank = From C in AllTheData
        Group C by C.ContentID, C.Description
             Into G
        Select New { ContentID = g.ContentID
                     Rank = (From rmax in g select rmax.rank).Max()
                     Description = g.Description
                   };
    

    But, if I understand you correctly, this would be very easy in SQL:

    Select ContentID, Max(Rank) As Rank, Description
    From (Select * From ComplicatedFullTextSearchQuery()) As Q
    Group By ContentID, Description
    

    I arranged this sql like your main query was a UDF; whatever your original query is, put it in the () on the second line. If performance matters, have a procedure/function put your original query into a temporary table, an then just do:

    Select ContentID, Max(Rank) As Rank, Description
    From TempTable    Group By ContentID, Description
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good afternoon, This should be an easy one. I've done the cookie-cutter default ASP.NET
This should a quick question for some easy rep. I'm doing some PHP Website
I know this should be an easy one, but I'm failing to make it
This should be easy, but I'm having trouble figuring out how to zoom an
This one should be easy. I have a Reviews table with records. The columns
This should be easy for many of you, but for me it's just another
This should be easy, but I'm not sure how to best go about it.
I think this should be easy, but it's evading me. I've got a many-to-many
This should hopefully be a simple one. I would like to add an extension
What advantages are there for using either XSLT or Linq to XML for HTML

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.