使用 LINQ 查询获取计数和不同项?
我们有下面的 SQL 表:
Tag
----------------------------------------
ID | tagName | UserID
----------------------------------------
01 | c++ | 03
02 | c# | 03
03 | asp.net | 04
04 | programming | 05
05 | vb | 07
06 | php | 07
07 | java | 12
08 | class | 12
TagRelation
----------------------------------------
ID | tagID | topicID
----------------------------------------
01 | 01 | 22
02 | 02 | 22
03 | 08 | 22
04 | 06 | 12
05 | 02 | 12
06 | 04 | 104
07 | 02 | 104
08 | 01 | 104
09 | 02 | 17
10 | 05 | 17
现在我想使用 LINQ 查询获取每个用户的 TagRelation
表中每个标签的重复次数。 我使用了以下查询,但它无法编译,它有一些错误。
DataClassesDataContext dbc = new DataClassesDataContext();
var query = from q in dbc.Tags
where q.UserID == this.UserID
from c in dbc.TagRelations
where c.tagID == q.id
select new
{
tagName = q.name.Distinct(),
repeatNumber = c.Count()
};
我该怎么做呢?谢谢。
编辑:实际上我期望以下结果:
result (if UserID==3)
-------------------------
tagName | repeatNumber
-------------------------
c++ | 02
c# | 01
We have below SQL tables :
Tag
----------------------------------------
ID | tagName | UserID
----------------------------------------
01 | c++ | 03
02 | c# | 03
03 | asp.net | 04
04 | programming | 05
05 | vb | 07
06 | php | 07
07 | java | 12
08 | class | 12
TagRelation
----------------------------------------
ID | tagID | topicID
----------------------------------------
01 | 01 | 22
02 | 02 | 22
03 | 08 | 22
04 | 06 | 12
05 | 02 | 12
06 | 04 | 104
07 | 02 | 104
08 | 01 | 104
09 | 02 | 17
10 | 05 | 17
now I want to get the number of repeating each tags in TagRelation
table for each users with LINQ query.
I've used the following query , but it doesn't compile, it has some errors.
DataClassesDataContext dbc = new DataClassesDataContext();
var query = from q in dbc.Tags
where q.UserID == this.UserID
from c in dbc.TagRelations
where c.tagID == q.id
select new
{
tagName = q.name.Distinct(),
repeatNumber = c.Count()
};
How can I do it? Thanks.
Edit: Actually I expected the following result:
result (if UserID==3)
-------------------------
tagName | repeatNumber
-------------------------
c++ | 02
c# | 01
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)