I have a model where the place has some descriptions, these descriptions are related to interests (place.description.interests). A user looking at a representation of a place is represented in the model as a user who also has a number of interests.
What I want to do is sort the description by overlapping interests (including zero overlap), where is my current Linq:
place dest = (from p in _db.places where p.short_name == id select p).Single(); return View(dest);
Now, the following will do what I want in SQL on the schema in question:
SELECT COUNT(interest_user.user_id) AS matches, description.* FROM description JOIN interest_description ON description.user_id = interest_description.user_id AND description.place_id = interest_description.place_id JOIN interest ON interest_description.interest_id = interest.interest_id LEFT JOIN interest_user ON interest.interest_id = interest_user.interest_id WHERE interest_user.user_id = 2 AND description.place_id = 1 GROUP BY interest_description.user_id, interest_description.place_id ORDER BY matches DESC
But I'm too new to Linq to find out how I will handle this correctly. Ideally, I could pull this out while still passing in a strongly typed model.
I succeeded:
var desc = from d in _db.descriptions from i in d.interests from u in i.users.DefaultIfEmpty() where d.place_id == PlaceID && (u.user_id == userID
(PlaceID and UserID are the arguments passed to the controller that controls this).
Simply put, given this linq, I just need to return the d ordered by account i.
my model 
source share