我有一个模型,其中一个地方有一些描述,这些描述与兴趣(place.description.interests)相关联。用户查看地点的视图在模型中以用户身份表示,他们也有许多兴趣。LINQ中找到重叠兴趣
我想要做的排序是重叠的利益(包括零重叠),那里有我当前的LINQ是描述:
place dest = (from p in _db.places
where p.short_name == id
select p).Single();
return View(dest);
现在,下面将做什么,我想在SQL上有问题的模式:
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
但我太新的Linq知道我将如何正确处理这个问题。理想情况下,我可以在传递强类型模型的同时将其取消。
到目前为止,我已经成功的:
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和用户名是传递给管理该控制器参数)。
简而言之,考虑到这个linq,我只需要返回d,按i计数。
我的模型
你能举出输入数据的例子吗?应该给他们什么结果? – 2011-06-17 06:21:23
好吧,我有一个ID为1的地方,ID为1的用户。我的用户有兴趣1,2,4。这个地方有1,2,3个描述,其中描述1有兴趣1,2,描述2有兴趣2,3,描述3有兴趣1,2,3,4。当用户1查看地点1时,说明应按3,1,2排序,因为3有三个重叠的兴趣,1有两个重叠的兴趣,2有一个重叠的兴趣。 – 2011-06-17 16:03:08