使用 LINQ 查询获取计数和不同项?

发布于 2024-11-04 17:48:04 字数 1412 浏览 0 评论 0原文

我们有下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾夏年华 2024-11-11 17:48:04
var q = ...
        group r by r.TagName into g;
        select new
        {
            TagName = g.Key,
            Count = g.Count()
        };

foreach (var i in q)
{
    string tagName = i.TagName;
    int count = i.Count;    
}    
var q = ...
        group r by r.TagName into g;
        select new
        {
            TagName = g.Key,
            Count = g.Count()
        };

foreach (var i in q)
{
    string tagName = i.TagName;
    int count = i.Count;    
}    
怪我鬧 2024-11-11 17:48:04
from t in dbc.Tags
where t.UserID == this.UserID
join r in dbc.TagRelations on t.id equals r.tagID
group r by t into g
select new
{
    tagName = g.Key.name,
    repeatNumber = g.Count()
};
from t in dbc.Tags
where t.UserID == this.UserID
join r in dbc.TagRelations on t.id equals r.tagID
group r by t into g
select new
{
    tagName = g.Key.name,
    repeatNumber = g.Count()
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文