views:

312

answers:

0

am new to LINQ and I have been able to write a few simple statements. But now I have a more complicated situation which I cannot figure out.

Basically, I am trying to write a LINQ to Objects statement where the relationship is a grandparent, parent, child relationship. (You could also call it a Master Detail relationship.)

In Legacy code here is a simplified version what I am trying to accomplish.

        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

If is it not clear one Location can have many Items and one Item can have many Coverages. I basically want to list the items and show the relationship between grandparent (Location), parent (Item) and child (Coverage).

Thanks for your help.


Thx for the response.

Here is what I came up with:

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

However, this just listed out all of the children (Coverages).

This is the result I am looking for:

Location

Item

Coverage

Item

Coverage

Coverage

Location

Item

Coverage

So, the data is Grouped By Location and Item.

Another way to look at it is a ListView or a Report. Where the Grouping is done by Location and Item.

An example is shown below

Location

Item

    Coverage

Item

    Coverage

    Coverage

Location

Item

    Coverage

Is this clear?