0
表這樣的關係記錄:如何讓所有的使用LINQ
id | userid | contentid
-------------------------
41 | 92 | 1187
42 | 92 | 1189
43 | 92 | 1190
44 | 92 | 1193
45 | 92 | 1200
46 | 92 | 1201
47 | 92 | 1202
48 | 104 | 1200
49 | 104 | 1201
50 | 104 | 1202
51 | 103 | 1200
52 | 103 | 1201
53 | 103 | 1202
,我試圖讓所有相關的內容ID與1202(例如)。
- 我會將所有添加了該內容的用戶分組。
- 根據除1202以外的內容ID對記錄進行分組後,必須按組排序。
不久,我想下面的列表:
1201 - count: 3
1200 - count: 3
1187 - count: 1
1189 - count: 1
1190 - count: 1
1193 - count: 1
我試着像下面是我想要的方式,但更需要做其他事情的查詢。
(from x in IRepository<ContentRelation>().Query().ToList()
where x.Content.Id == content.Id
group x by x.GUser.Id into c
select new
{
a = c.Key,
b = (from d in IRepository<ContentRelation>().Query()
where d.GUser.Id == c.Key && d.Content.Id != content.Id
select d)
})
編輯: 我得到了我想要與下面的查詢,但林不知道這是正確的做法:
var q = DependencyResolver.Current.GetService<IRepository<ContentRelation>>().Query();
List<int> gh = new List<int>();
foreach (var item in q.Where(x => x.Content.Id == content.Id).GroupBy(x => x.GUser.Id).Select(x => x.Key))
{
foreach (var a in q.Where(x => x.GUser.Id == item && x.Content.Id != content.Id).ToList())
{
gh.Add(a.Content.Id);
}
}
foreach (var hhj in gh.GroupBy(x => x).OrderByDescending(x => x.Count()))
{
Response.Write(hhj.Key + "-" + hhj.Count()+ "<br />");
}