gpt4 book ai didi

vb.net - LINQ to Objects - 祖 parent 、 parent 、子女关系

转载 作者:行者123 更新时间:2023-12-04 23:54:12 26 4
gpt4 key购买 nike

我是 LINQ 的新手,我已经能够编写一些简单的语句。但是现在我有一个更复杂的情况,我无法弄清楚。

基本上,我正在尝试编写 LINQ to Objects 语句,其中关系是祖 parent 、 parent 、子女关系。 (您也可以将其称为主从关系。)

在 Legacy 代码中,这里是我试图完成的简化版本。

        Dim coverages As New List(Of Coverage)
Dim coverage As Coverage

For Each rl In oClaimsPolicy.RiskLocations

coverage = New Coverage
coverage.Level = "Location"

'Get rl detail detail
coverages.Add(coverage)

For Each ri In rl.RiskItems

coverage = New Coverage
coverage.Level = "Item"

'Get ri detail
coverages.Add(coverage)

For Each rc In ri.RiskCoverages

coverage = New Coverage
coverage.Level = "Coverage"

'Get rc detail here
coverages.Add(coverage)

Next

Next

Next

如果不清楚,一个Location可以有多个Items,一个Item可以有多个Coverages。我基本上想列出项目并显示祖 parent (Location)、 parent (Item) 和 child (Coverage) 之间的关系。

更新:

这是我想出的:

Dim coverages = oClaimsPolicy.RiskLocations. _
SelectMany(Function(rl) rl.RiskItems. _
SelectMany(Function(ri) ri.RiskCoverages. _
Select(Function(rc) New Coverage With {.Level = "Coverage"})))

然而,这只是列出了所有子项(Coverages)。

这是我正在寻找的结果:

Location
Item
Coverage
Item
Coverage
Coverage
Location
Item
Coverage

因此,数据按位置和项目分组。

另一种查看方式是 ListView 或 Report。分组是按位置和项目完成的。

下面是一个例子

Location
Item
Coverage
Item
Coverage
Coverage
Location
Item
Coverage

清楚了吗?

最佳答案

当我去测试它时,你的原始查询没有编译。这是一个版本:

Dim coverages = _
From rl In oClaimsPolicy.RiskLocations _
From ri in rl.RiskItems _
From rc in ri.RiskCoverages _
Select New Coverage With {.Level = "Coverage"}

现在,要获得您想要的结果,请尝试这样做:

dim create As Func(Of String, IEnumerable(Of Coverage)) = _
Function (level) New Coverage() _
{ New Coverage With { .Level = level } }

Dim coverages = _
From rl In oClaimsPolicy.RiskLocations _
From cl In create("Location").Concat( _
From ri In rl.RiskItems _
From ci In create(" Item").Concat( _
From rc In ri.RiskCoverages _
From cc In create(" Coverage") _
Select cc) _
Select ci) _
Select cl

它是一堆嵌套的 SelectManyConcat 表达式,它生成一个 Coverage 对象列表,如下所示:

Location
Item
Coverage
Location
Item
Coverage
Item
Coverage

这就是你想要的吗?

关于vb.net - LINQ to Objects - 祖 parent 、 parent 、子女关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2193159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com