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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:10:08+00:00 2026-05-18T09:10:08+00:00

Is there a way to write a query in LINQ to return the count

  • 0

Is there a way to write a query in LINQ to return the count of the search terms found in a field(s)

Basically, I want this to work:

var matches = from t in _db.Books
                          let score = GetScore(t, searchterms)
                          where score >= 1
                          orderby score descending
                          select t;

public static int GetScore(Book b, params string[] searchterms)
    {
        int count = 0;
        foreach (string term in searchterms)
        {
            if (b.Title.Contains(term))
                count++;
        }
        return count;
    }

But, of course, that can’t work.
Can my little GetScore function be translated into LINQ?

Thanks.

EDIT: I would also prefer to have the score accessible. Ideally I will be selecting my results into a SearchResults class (for the View) that would contain some Book info and the Book’s score from the query. To update my query, it’d be something like this:

var matches = from t in _db.Books
                          let score = GetScore(t, searchterms)
                          where score >= 1
                          orderby score descending
                          select new SearchResult
                                                {
                                                    Title = t.Title,
                                                    Type = "Book",
                                                    Link = "Books/Details/" + t.BookID,
                                                    Score = score
                                                };

I’m sorry I wasn’t more clear originally.

  • 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-18T09:10:09+00:00Added an answer on May 18, 2026 at 9:10 am

    You can’t do what you want to do without issuing multiple queries to the database – essentially one per search term. If you are happy to do that, then here is an easy way to do it:

    var terms = new [] { "s", "t", "r", "e", "b", "c", };
    
    var ids =
        from term in terms
        from id in _db.Books
            .Where(book => book.Title.Contains(term))
            .Select(book => book.Id)
        group term by id into gts
        orderby gts.Count() descending
        select gts.Key;
    
    var selectedIds = ids.Take(50).ToArray();
    
    var query =
        from book in _db.Books
        where selectedIds.Contains(book.Id)
        select book;
    

    I wrote the ids to return a list of ids sorted by those that match the most terms first. This was to most closely get the same kind of result that you wanted in your question. I then decided to use a Take(50) to get the top 50 results. You can obviously change this strategy to suit your needs, but you must end up with an array of ids to use in the final query.

    I hope this helps.


    EDIT: based on OP’s edit.

    Here’s how to query with the score included:

    var terms = new [] { "s", "t", "r", "e", "b", "c", "l", "i", };
    
    var idScores =
        from term in terms
        from id in _db.Books
            .Where(book => book.Title.Contains(term))
            .Select(book => book.BookID)
        group term by id into gts
        select new
        {
            Id = gts.Key,
            Score = gts.Count(),
        };
    
    var selectedIds = idScores.Select(x => x.Id).Take(50).ToArray();
    
    var selectedBooks =
        from book in _db.Books
        where selectedIds.Contains(book.BookID)
        select book;
    
    var query =
        from b in selectedBooks.ToArray()
        join x in idScores on b.BookID equals x.Id
        orderby x.Score descending
        select new
        {
            Title = b.Title,
            Type = "Book",
            Link = "Books/Details/" + b.BookID,
            Score = x.Score,
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way to write this code? I want to show a
Using standard mysql functions is there a way to write a query that will
Is there any way to Write files to App_Data under medium trust hack? Im
Is there a way to write an enumeration that can be extended. I have
Is there a way to write log4j logging events to a log file that
Is there an easy way write to a file asynchronously in Python? I know
Is there any way to write a Visual Studio Project file easy or do
Calling all C macro gurus... Is there any way to write a C macro
Is there a way I can write text to a file from a certain
Is there a good way to write F# scripts that execute stand-alone without compiling?

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.