【问题标题】:Stuck on a subquery that is grouping, in Linq`卡在 Linq 中正在分组的子查询上
【发布时间】:2009-05-19 01:58:40
【问题描述】:

我有一些 Linq 代码,它工作正常。这是一个在Where 子句中有一个子查询的查询。此子查询正在执行 groupby。效果很好。

问题是我不知道如何将子查询中的一个结果从子查询中抓取到父查询中。

首先,这是代码。之后,我将解释我要提取的数据。

    var results = (from a in db.tblProducts
               where (from r in db.tblReviews
                      where r.IdUserModified == 1
                      group r by
                          new
                              {
                                  r.tblAddress.IdProductCode_Alpha,
                                  r.tblAddress.IdProductCode_Beta,
                                  r.tblAddress.IdProductCode_Gamma
                              }
                      into productGroup
                          orderby productGroup.Count() descending
                          select
                          new
                              {
                                  productGroup.Key.IdProductCode_Alpha,
                                  productGroup.Key.IdProductCode_Beta,
                                  productGroup.Key.IdProductCode_Gamma,
                                  ReviewCount = productGroup.Count()
                              }).Take(3)
                   .Any(
                   r =>
                   r.IdProductCode_Alpha== a.IdProductCode_Alpha&& 
                       r.IdProductCode_Beta== a.IdProductCode_Beta&&
                       r.IdProductCode_Gamma== a.IdProductCode_Gamma)
               where a.ProductFirstName == ""
               select new {a.IdProduct, a.FullName}).ToList();

好的。我更改了一些字段和表的名称以保护无辜者。 :)

请看最后一行:-

select new {a.IdProduct, a.FullName}).ToList();

我希望包含 ReviewCount(来自子查询)。我只是不确定如何。

为了帮助理解问题,这就是数据的样子。

子查询

IdProductCode_Alpha = 1,IdProductCode_Beta = 2,IdProductCode_Gamma = 3,ReviewCount = 10 ... 第 2 行 ... ...第 3 行 ...

父查询

IdProduct = 69, FullName = 'Jon Skeet's Wonder Balm'

所以子查询获取了我需要的实际数据。父查询根据子查询过滤器确定正确的产品。

编辑 1:架构

tbl产品

  • IdProductCode
  • 全名
  • 产品名字

tblReviews(每个产品有零到多个评论)

  • IdProduct
  • IdProductCode_Alpha(可以为空)
  • IdProductCode_Beta(可以为空)
  • IdProductCode_Gamma(可以为空)
  • IdPerson

所以我试图找出一个人评论过的前 3 种产品。

linq 运行良好...除了我不知道如何在父查询中包含 COUNT(即从子查询中提取结果)。

干杯:)

【问题讨论】:

  • 我想最好将模式与示例数据一起提供,并且预期的输出将帮助人们形成查询。
  • 我猜架构不完整。你说的人是什么意思?如果是数据库,你会如何编写 SQL?
  • 是的。这是不完整的,因为它都是伪代码,我正在尝试即时执行此操作。我不允许将任何真实代码发布到公共领域(合同等)我知道我知道:(
  • 我现在通过在评论表中添加一个 Person 来稍微修改架构。每条评论都是由一个人发布的。每个产品都可以发布零到多条评论。

标签: linq


【解决方案1】:

我自己搞定的。请注意查询开头的双 from,然后将 Any() 替换为 Where() 子句。

var results = (from a in db.tblProducts
               from g in (
                  from r in db.tblReviews
                  where r.IdUserModified == 1
                  group r by
                      new
                          {
                              r.tblAddress.IdProductCode_Alpha,
                              r.tblAddress.IdProductCode_Beta,
                              r.tblAddress.IdProductCode_Gamma
                          }
                  into productGroup
                      orderby productGroup.Count() descending
                      select
                      new
                          {
                              productGroup.Key.IdProductCode_Alpha,
                              productGroup.Key.IdProductCode_Beta,
                              productGroup.Key.IdProductCode_Gamma,
                              ReviewCount = productGroup.Count()
                          })
                  .Take(3)
           Where(g.IdProductCode_Alpha== a.IdProductCode_Alpha&& 
               g.IdProductCode_Beta== a.IdProductCode_Beta&&
               g.IdProductCode_Gamma== a.IdProductCode_Gamma)
           where a.ProductFirstName == ""
           select new {a.IdProduct, a.FullName, g.ReviewCount}).ToList();

【讨论】:

    【解决方案2】:

    虽然我不完全了解 LINQ,但是 JOIN 不起作用吗?
    我知道我的回答无济于事,但看起来您需要与内表进行 JOIN(?)。

    【讨论】:

      【解决方案3】:

      关于架构和连接,我同意 shahkalpesh。

      你应该可以重构...

       r => r.IdProductCode_Alpha == a.IdProductCode_Alpha  && 
            r.IdProductCode_Beta == a.IdProductCode_Beta  &&
            r.IdProductCode_Gamma == a.IdProductCode_Gamma
      

      tblProducts 进行内部连接。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-17
        • 1970-01-01
        • 2015-12-13
        • 1970-01-01
        相关资源
        最近更新 更多