【问题标题】:LINQ to Entities, join two tables, then group and take sums of columns from both tablesLINQ to Entities,连接两个表,然后对两个表中的列进行分组并求和
【发布时间】:2011-02-22 00:50:14
【问题描述】:

正如标题所说,我的目标是在多个列上连接两个表(目标和事务),然后对该连接的结果进行分组,并对两个表中的列值求和。以下查询仅允许访问联接中 FIRST 表中的列!

 var actualsVsTargets = (from target in ObjectContext.PipelineTargets
                 join transaction in ObjectContext.Transactions on
                    new
                    {
                        target.Year,
                        target.Quarter,
                        target.StateID,
                        target.ProductGroup.TeamId
                    } equals new
                    {
                        transaction.Year,
                        transaction.Quarter,
                        transaction.StateID,
                        transaction.Product.ProductGroup.TeamId
                    }   
                 where target.Year == year && target.ProductGroup.TeamId == teamId
                 group target by new
                                     {
                                         target.ProductGroupID,
                                         target.StateID,
                                         target.Year
                                     }
                 into targetGroup
                 select new
                            {
                                // this works fine (accessing target column)
                                TargetL1 = targetGroup.Sum(target => target.Level1_Target,
                                // this doesn't work (accessing transaction column)
                                ActualL1 = targetGroup.Sum(trans => trans.Level1_Total)
                            }).SingleOrDefault();

如下所示,这在 T-SQL 中实现起来很简单,(大致):

   SELECT
    targets.Year, 
    targets.StateID, 
    SUM(targets.Level1_Target) L1_Target, -- get the sum of targets
    SUM(transactions.Level1_Total) L1_Total -- get the sum of transactions
  FROM PipelineTargets targets 
  JOIN Transactions transactions 
    JOIN Products prods ON 
        transactions.ProductID = prods.ProductID 
    ON 
        targets.Year = transactions.Year and 
        targets.Quarter = transactions.Quarter and 
        targets.StateID = transactions.StateID and 
        prods.ProductGroupID = targets.ProductGroupID
  WHERE targets.Year = '2010' and targets.StateID = 1
  GROUP BY targets.Year, targets.StateID, targets.ProductGroupID

如何在 LINQ 中执行此操作?

【问题讨论】:

    标签: c# linq entity-framework linq-to-entities


    【解决方案1】:

    事务变量超出范围。如果将其包含在分组结果中,则可以使用它。

    将您的 group by 子句更改为:

    group new
            {
                target,
                transaction
            }
            by new
            {
                target.ProductGroupID,
                target.StateID,
                target.Year
            } into grouped
    

    然后你的 select 子句可以这样做:

    select new
            {
                TargetL1 = grouped.Sum(groupedThing => groupedThing.target.Level1_Target,
                ActualL1 = grouped.Sum(trans => groupedThing.transaction.Level1_Total)
            }).SingleOrDefault();
    

    【讨论】:

    • 谢谢,无法弄清楚“group new {target, transaction}”部分。现在很有意义。
    【解决方案2】:

    首先,您应该为相关外键设置导航属性(例如,目标与交易的 1:N 关系,以及交易与产品的 1:N 关系)。如果设置正确,则数据库设计/EF 模式应该可以从您手中完成大部分工作。之后你可以去:

    (from target in targets
      where target.Year == year && target.ProductGroup.TeamId == teamId
      group target by new 
                          { 
                            target.ProductGroupID, 
                            target.StateID, 
                            target.Year 
                          } into targetGroup
      select new { Key = targetGroup.Key,
                   TargetL1 = targetGroup.Sum(target => target.Level1_Target),
                   ActualL1 = targetGroup.SelectMany(tg => tg.Transactions)
                                         .Sum(trans => trans.Level1_Total)
                 });
    

    但是,我无法对其进行测试,因此可能会出现错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-10
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多