我有一個模型,其中一個地方有一些描述,這些描述與興趣(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