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 8382323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:54:21+00:00 2026-06-09T16:54:21+00:00

Previously in SQL I would use something like this to rank my search results:

  • 0

Previously in SQL I would use something like this to rank my search results:

        --search product name
    SELECT tblProduct.ProductID, 20 AS Ranking
    FROM tblProduct
    INNER JOIN tblManufacturer ON tblProduct.ManufacturerID=tblManufacturer.ManufacturerID
    LEFT OUTER JOIN tblProductCollection ON tblProduct.CollectionID=tblProductCollection.CollectionID
    WHERE tblManufacturer.Name + ISNULL(' ' + tblProductCollection.CollectionName, '') + ' ' + tblProduct.Name LIKE '%' + @term + '%'  AND tblProduct.Active = 1
    UNION ALL

    --search product exact name
    SELECT tblProduct.ProductID, 200 AS Ranking
    FROM tblProduct WHERE Name = '%' + term  AND tblProduct.Active = 1
    UNION ALL

This example says if your search term is contained in the name: 20 is the rank, if you match the name exactly: 200 is the rank. Union the tables together, order by Ranking(descending) and hey presto!

I’m trying to do this in LINQ this time round and am unsure how to go about doing it, to be honest I’m unsure my previous example was the best way to do it originally.

So, I have a Product Entity mapped to my database and I’ve added a property in my partial class called SearchRanking:

            var query = from p in db.Products
                    where p.Name.Contains(term)
                    select p;

        var query2 = from p in db.Products
                     where p.Name.ToLower() == term
                     select p;

Somehow I need to set the properties like so:

            var query = from p in db.Products
                    where p.Name.Contains(term)
                    select p, p.SearchRanking = 20;

        var query2 = from p in db.Products
                     where p.Name.ToLower() == term
                     select p, p.SearchRanking = 200;

Am I on the right track?

  • 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-06-09T16:54:22+00:00Added an answer on June 9, 2026 at 4:54 pm

    If you’re wanting to create a new anonymous type you could do this:

     var foundProducts = (from p in products
                                     where p.Name.Contains(term)
                                     select new Product  
                                         {
                                             ProductId = p.ProductId,
                                             Category =  p.Category,
                                             Brand =  p.Brand,
                                             SearchRanking = p.Name.ToLower() == term ? 200 : 20
                                         }).OrderBy(s => s.SearchRanking).Take(20);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my previous applications when I used linq-to-sql I would always use one class
I would like to use a Groovy closure to process data coming from a
I'd like to use a single SQL query (in MySQL) to find the record
I have a SELECT statement which I would like to optimize. The mysql -
Previously I did a Tutorial on OpenGL-ES 1.0. For reference, this can be found
Previously, I have an app that uses core data. I use same store url
After reading previous questions about this error, it seems like all of them conclude
Let's say that you are limited to use only this syntax (you can't use
It appears my SQL isn't limiting results based on price . In my previous
sorry for my English. I previously posted this question as: Multiple ViewModels request same

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.