Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding Overlapping Interests in LINQ
    primarykey
    data
    text
    <p>I have a model where a place has some descriptions, those descriptions are associated with interests (place.description.interests). A user looking at the view for a place is represented in the model as a user, who also has a number of interests.</p> <p>What I want to do is sort the description by overlapping interests (including zero overlapping), where my current Linq is:</p> <pre><code>place dest = (from p in _db.places where p.short_name == id select p).Single(); return View(dest); </code></pre> <p>Now the following will do what I want in SQL on the schema in question:</p> <pre><code>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 </code></pre> <p>But I'm too new to Linq to know how I would handle this correctly. Ideally I could pull this off while still passing in a strongly typed model.</p> <p>I have managed this so far:</p> <pre><code>var desc = from d in _db.descriptions from i in d.interests from u in i.users.DefaultIfEmpty() where d.place_id == PlaceID &amp;&amp; (u.user_id == userID </code></pre> <p>(PlaceID and UserID are arguments passed to the controller that is managing this).</p> <p>Simply put, given this linq, I just need to return d, ordered by a count of i.</p> <p><strong>my model</strong> <img src="https://i.stack.imgur.com/lu2Yw.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload