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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:06:18+00:00 2026-06-15T04:06:18+00:00

I wrote an extension method to try and count the words in a string

  • 0

I wrote an extension method to try and count the words in a string that appear in a description. Here is the function (for examples sake, searching “soft” I’d want to see Microsoft so calling it keywords may be incorrect, but I wanted to communicate that ahead. Keyword was just the only thing that came to mind at the time):

public static int KeywordCount(this string str, string Phrase)
{
    string[] elements = Phrase.ToUpper().Split(' ');
    int count = 0;
    foreach (string element in elements)
    {
        if (str.ToUpper().Contains(element))
            count++;
    }
    return count;
}

I’m trying to then go through a list of items and include only the items where at least something hits, but also sorting it based on the number of hits (if all keywords are in the description, it needs to be at the top and so forth.

What I have so far is:

 selection.AddRange(all.Where(c => c.Description.KeywordCount(query) > 0));

How can I then sort based on the return value of KeywordCount? I also have a feeling that the direction I’m going will result in doing the KeywordCount search 1x per all items and 1x extra for items with a count > 0, so if there is a way to do the compare and sort at the same time, I’d like to know how.

  • 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-15T04:06:20+00:00Added an answer on June 15, 2026 at 4:06 am

    Do a select to get the value and the count first, then filter on count, then order by the count, then select just the value.

    selection.AddRange(
        all.Select(c=>new {Val = c, Count = c.Description.KeywordCount(query))
           .Where(t => t.Count > 0))
           .OrderBy(t => t.Count)
           .Select(t => c.Val);
    

    This way the KeywordCount is called once per item.

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

Sidebar

Related Questions

I wrote a custom ordering LINQ extension method as below but I think it
I'm trying to write an extension method for objects that will dump the structure
I wrote this extension method (which compiles): public static IEnumerable<J> Flatten<T, J>(this IEnumerable<T> @this)
I wrote an extension method which returns me 2-dimensional array of YUV values from
I'd like to write an extension method for the .NET String class. I'd like
I wrote an extension in VB.NET for StringBuilder to add a AppendFormattedLine method (so
I'm having troubles in writing a static Deflate extension method, that i would use
I wrote the following extension method to get an element from a dictionary, or
I'm trying to write an extension method to insert data into a dictionary of
I recently found out about C# extension methods and wrote this one: /// <summary>

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.