开发者

LINQ to Objects - Grandparent, Parent, Child Relationship

I 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 wr开发者_开发百科ite 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).

Update:

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?


Your original query didn't compile when I went to test it. Here's a version that does:

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

Now, to get the kind of results that you want try doing this:

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

It's a bunch of nested SelectMany & Concat expressions and it produces a list of Coverage objects like this:

Location
  Item
    Coverage
Location
  Item
    Coverage
  Item
    Coverage

Is this what you're after?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜