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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:19:55+00:00 2026-05-17T16:19:55+00:00

I have an ObservableCollection, which holds a Person object. I have a search feature

  • 0

I have an ObservableCollection, which holds a Person object. I have a search feature in my application, and would like to display the most relevant results at the top. What would be the most efficient way of doing this? My current search method simply calls the contains method:

 var results = (from s in userList
               where s.Name.Contains(query)
               select s).ToList();

This works fine, but the results are ordered in the same order they appear within userList. If I search for Pete, then it should first display Pete, then Peter then Peter Smith etc..
It doesn’t have to be too complicated as it will only be dealing with a couple thousand (max) results. My naive approach was to first do s.Name == query, display that item (if any), then perform the s.Name.Contains(query), remove the matched item and append it to the previous matched result. However, this seems a bit all over the place and so is there a better way? thanks
(ps – only the name will be used in searching, and I can’t use SQL methods)

  • 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-17T16:19:56+00:00Added an answer on May 17, 2026 at 4:19 pm

    You can make a single routine that provides a name and a query string, and returns an integer value.

    Once you have that, just return via order by:

    int QueryOrder(string query, string name)
    {
         if (name == query)
             return -1;
         if (name.Contains(query))
             return 0;
    
         return 1; 
    }
    

    Then do:

    var results = userList.OrderBy(s => QueryOrder(query, s.Name));
    

    The nice thing about this approach is that, later, you could extend the routine to provide more details, allowing you to sort by how “good” of a match you receive. For example, “Pete” -> “Peter” is probably a better match than “Pete” -> “Peter Smith”, so you could have your logic return a different value for the different options…

    If you need to remove “non-Pete” matches, you could exclude out with a Where clause, as well.

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

Sidebar

Related Questions

I would like to use a WrapPanel. I have a ObservableCollection with ViewModels which
I have this ListBox which is bound to an ObservableCollection. Each object in the
I have a thread safe observable collection replacement which I would like to write
I have a ObservableCollection which holds records of custom type Item. I use that
I have a ObservableCollection<Object> that holds two different types of objects: Directory and File
in my Silverlight 4 application, I have an ObservableCollection which consists of objects of
I have a Silverlight delegate which gets an ObservableCollection in the EventArgs Result. The
I have WPF ListBox which is bound to a ObservableCollection, when the collection changes,
I have a view model with a property Fields which is an ObservableCollection<FieldVM> .
I have an ObservableCollection which is dataContext for my treeview when I try to

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.