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.
Make a hash set with the identities from the tags:
Then you can count the tags that exist in the hash set: