using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
var userA = new User {Location = "abc"};
var userB = new User {Location = "abc"};
var userC = new User {Location = "abc"};
var userD = new User {Location = "abc"};
var userE = new User {Location = "abc"};
context.UserInterests.Add(new UserInterest {InterestID = 1, User = userA});
context.UserInterests.Add(new UserInterest {InterestID = 2, User = userA});
context.UserInterests.Add(new UserInterest {InterestID = 3, User = userA});
context.UserInterests.Add(new UserInterest {InterestID = 1, User = userB});
context.UserInterests.Add(new UserInterest {InterestID = 3, User = userB});
context.UserInterests.Add(new UserInterest {InterestID = 1, User = userC});
context.UserInterests.Add(new UserInterest {InterestID = 4, User = userC});
context.UserInterests.Add(new UserInterest {InterestID = 1, User = userD});
context.UserInterests.Add(new UserInterest {InterestID = 3, User = userD});
context.UserInterests.Add(new UserInterest {InterestID = 1, User = userE});
context.UserInterests.Add(new UserInterest {InterestID = 4, User = userE});
using (var context = new EntityContext())
var data = context.UserInterests
.Join(context.UserInterests, x => x.InterestID, x => x.InterestID, (a, b) => new { CurrentInterest = a, MatchedInterest = b })
.Where(x => x.CurrentInterest.UserID == currentPersonId && x.MatchedInterest.UserID != currentPersonId)
.Select(x => x.MatchedInterest.User)
.Where(x => x.Location.StartsWith("a"))
.OrderBy(u => Guid.NewGuid())
FiddleHelper.WriteTable("Matched Users to UserID 1", data);
var list = context.UserInterests.Include(x=>x.User).ToList();
FiddleHelper.WriteTable("All UserInterests", list);
public class EntityContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(FiddleHelper.GetConnectionStringSqlServer());
public DbSet<User> Users { get; set; }
public DbSet<UserInterest> UserInterests { get; set; }
public int UserID { get; set; }
public string Location { get; set; }
public List<UserInterest> Interests { get; set; }
public class UserInterest
public int UserInterestID { get; set; }
public int InterestID { get; set; }
public int UserID { get; set; }
public User User { get; set; }