我有一个这样的表结构...
当我将其导入 Entity Framework 时,它看起来像这样......
我需要做的是从 LINQ 构建一个查询,该查询将返回每个独特商店的列表,其中填充了喜欢该商店的人的列表。 (简单,对吧?)
要点: 我需要将列表过滤为作为列表传递给 linq 查询的人的 friend 列表(来自 facebook,因此关系不在数据库中)...
还有一件事:
如果商店是请求数据的人的最爱,我需要返回(如下所示的 uid
)
好的,还有一件事: 我需要返回按喜欢某项的 friend 数量从多到少排序的列表(下面的 ui 在这方面是错误的)
这是我需要的linq查询的方法签名
public List<Store> GetTopStoresFilteredByFriends
(int uid, List<int> friends, int size = 10){
}
要返回如下所示的用户界面...
最佳答案
此查询为您提供了每个独特商店的列表,其中包含喜欢该商店的人的列表,该商店也被您正在传递的列表中的至少一个 friend 喜欢喜欢商店加上一个标志,以显示请求数据的人是否喜欢每家商店:
var query = (from s in ctx.Stores.Include("People")
from p in s.People
let n = friends.Sum(f => s.People.Count(item => item.PersonID == f))
where friends.Any(f => f == p.PersonID)
select new {
TheStore = s,
IsFavorite = s.People.Any(ppl => ppl.PersonID == uid),
N = n
})
.Distinct()
.OrderByDescending(o => o.N);
关于c# - 多对多按关系计数排序( Entity Framework ,Linq),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3910601/