【问题标题】:retrieving nested collections with LINQ to SQL使用 LINQ to SQL 检索嵌套集合
【发布时间】:2014-06-22 18:25:51
【问题描述】:

我正在尝试取回一个 TransferId 列表,为每个传输一个 ChargeId 列表,并为每个传输一个 ReferralMatches 列表

这就是我所拥有的

(from c in Commissions
where c.TransferStatus == "Paid"
where c.AdminHasReleased == false
join r in ReferralMatches on c.ReferralMatchId equals r.ReferralMatchId
group c by new { c.TransferId } into grp
select new { 
   TransferId = grp.Key.TransferId,
   Charges = from c in grp
             group c by c.ChargeId into grp2
             select new { 
                ChargeId = grp2.Key, 
                Referrals = grp2 }
})

这很有效并且非常接近。它拉回看起来像这样的东西:

这看起来像是属于 TransferId 的费用,但我需要的是属于属于 transferId 的费用的 ReferralMatches。我尝试了另一个选择来拉入“r”,但遇到了错误。

我认为 LINQ 人员将能够从这篇文章中收集他们需要的信息,但如果需要更多信息,请告诉我。谢谢。

编辑,添加表格样本

这两个扩展表是我必须使用的。它可能没有用,但请记住,ReferralMatch 表也有 ChargeId。一个chargeId 可以涵盖多个ReferralMatches,但是一旦资金可用,就会发生银行转帐……发生这种情况时,会在Commissions 表中创建记录。

所以我要查找的是 TransferIds 列表,foreach Id 是chargeIds 列表,foreach chargeId 是 ReferralMatches 列表...最里面的 ReferralMatches 列表将是该表中的完整记录。

编辑,更多尝试

这是我最近的尝试

from c in Commissions
where c.TransferStatus == "paid"
group c by c.TransferId into transferGroup
select new {
    TransferId = transferGroup.Key,
    Charges = from c in transferGroup
              join r in ReferralMatches on c.ReferralMatchId equals r.ReferralMatchId
              group c by c.ChargeId into chargeGroup
              select new {
                ChargeId = chargeGroup.Key,
                Referrals = from r in chargeGroup
                            select new {
                                Referral = r
                            }
              }
}

这会产生如下内容:

但除非我读错了,否则最里面的项目仍然是没有意义的佣金表。我需要它是 ChargeId 为 [whatever]

的 ReferralMatches

【问题讨论】:

  • 我不清楚您到底想要实现什么 - 示例结果集看起来像一个 TransferIds 列表,对于每个 TransferId 一个 ChargeIds 列表(尽管每个 Transfer 组的示例只有一个),在其中,每个 ChargeId 的 ReferralMatches 列表。您能否指出示例数据的预期结果对象图应该是什么样子?能否也贴一下 Commissions 和 ReferralMatches 的对象结构?
  • 是的,我会发布表格。示例结果确实是 TransferIds 的列表,foreach 是 ChargeIds 的列表......但其中不正确......我知道它说的是 Referrals,但这些记录来自 Commissions 表而不是 ReferralMatch 表。我需要最里面的集合是基于 ChargeId 的完整的 ReferralMatches 集合。发布表格...
  • 太好了,感谢您的更新。还有一件事,从对象结构来看,每次Transfer好像永远只有一个ChargeId,所以每次Transfer永远不会有一个list,对吗?
  • 我认为如果你将这个庞然大物分解成几个较小的查询,它会更有意义并且更容易维护。
  • 抱歉耽搁了。关于每次传输一个chargeId,不,这只是显示这种方式,因为我那里只有几个测试行。将有一个或多个转介属于收费,也将有一个或多个收费属于转移。这种情况是为许多推荐支付或收费。一旦付款清除,就会发生银行转帐。银行转账每天发生,可以支付当天结清的任何费用。所以:单次转移有多个费用,其中包含多个行项目(推荐)

标签: c# sql linq


【解决方案1】:

您可能需要将表达式分成两部分并通过第一个表达式中的引用,然后按如下方式对第二个表达式进行分组:

        var commissionAndReferrals = 
            from c in Commissions
            where c.TransferStatus == "Paid"
            where c.AdminHasReleased == false
            join r in ReferralMatches on c.ReferralMatchId equals r.ReferralMatchId
            select new { Commisson = c, Referral = r };

        var result =
            from cAndR in commissionAndReferrals
            group cAndR by cAndR.Commisson.TransferId into transferGroup
            select new
            {
                TransferId = transferGroup.Key,
                Charges = from c in transferGroup
                          group c by c.Commisson.ChargeId into chargeGroup
                          select new
                          {
                              ChargeId = chargeGroup.Key,
                              Referrals = chargeGroup.Select(x => x.Referral)
                          }
            };

【讨论】:

  • 感谢您的回复,如果能在一个数据库快照中完成所有这些工作,我会非常高兴,但如果没有别的,我已经学到了很多关于 LINQ 分组的知识。我想我会做一些性能测试,看看这种方式的优缺点是什么,或者只是使用几个点击来构建一个具有多种更简单方法的复杂对象......或者就此而言,也许最好推将所有数据放入单独的对象中,忘记嵌套,只需在视图级别使用 LINQ 来收集和排序数据...嗯
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 2010-09-18
相关资源
最近更新 更多