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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:10:30+00:00 2026-05-27T10:10:30+00:00

Here is my user object: public class User { public int UserId { get;

  • 0

Here is my user object:

public class User
{
    public int UserId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

In my controller, I select the currently logged in user based on their user id:

User CurrentUser = Db.Users.Find((int)Session["UserId"]);

Next, I select a list of all users except the currently logged in user:

List<User> Users = Db.Users.Where(u => u.UserId != CurrentUser.UserId).ToList();

Here’s where it gets really confusing: I want to sort this list of users based on how closely their lists of tags compare to the currently logged in user’s list of tags. For instance, assume User A – who is currently logged in – has the following list of tags:

List<Tag> CurrentUserTags = new List<Tag>
{
    new Tag
    {
        TagId = 1
    },
    new Tag
    {
        TagId = 2
    },
    new Tag
    {
        TagId = 3
    }
}

User B has tags 1 and 2. User C only has tag 1. I want to sort the list of users as follows: User B then User C, because User B’s list of tags compares more closely to User A’s list of tags than User C’s list.

I really hope this makes sense, as I am completely stuck.

  • 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-27T10:10:30+00:00Added an answer on May 27, 2026 at 10:10 am

    Make a hash set with the identities from the tags:

    var currentTags = new HashSet<int>(CurrentUserTags.Select(t => t.TagId));
    

    Then you can count the tags that exist in the hash set:

    Users = Users.OrderByDescending(
      u => u.Tags.Count(t => currentTags.Contains(t.TagId))
    ).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application has the following entities: public class User { public virtual int UserID
Here's the relevant code: Public class User.cs: public void FindByID(int id) { Parser parser
I have a parent-child relationship: @Entity @Table(name = user) public final class User {
Here's my user model: class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable,
I have an interface like this: public interface IUser{ //some properties here T ToDerived(User
I have a class user which looks like this: public class User { public
I have User object with a list of groups that it belongs to: public
Here's my code: namespace RequestApi { public partial class MainPage : PhoneApplicationPage { private
I'm building an excel template (*.xlt) for a user here, and one of the
I'm utilizing the code posted by Jesper Palm here: Make user control display outside

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.