0

I am trying to figure out the functionality of ToLookup method here in the code.It's using grouping somehow to return a list. Can somebody just tell me how this is working or just guide me in the right direction:

.ToLookup(
  c => new 
  {
      c.CoverageType.Code, 
      c.CoverageType.Mnemonic, 
      c.CoverageType.Description, 
      c.CoverageType.CoverageLevel, 
      c.CoverageType.CoveragePosition
  })
.Select(
  t => new CoverageOption
  {
    CoverageType =
      new CoverageType
      {
          Code = t.Key.Code, 
          Mnemonic = t.Key.Mnemonic, 
          Description = t.Key.Description, 
          CoverageLevel = t.Key.CoverageLevel, 
          CoveragePosition = t.Key.CoveragePosition
      },
    Limits =
      (from Coverage c in t select c.Limit).GroupBy(l => l.Code)
        .Select(gr => gr.First()), 
    Deductibles =
      (from Coverage c in t select c.Deductible).GroupBy(d => d.Code)
        .Select(gr => gr.First())
})
.OrderBy(t => t.CoverageType.CoveragePosition);
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
Charu
  • 2,679
  • 6
  • 35
  • 52
  • Which bit don't you understand? You use `ToLookup` to create a one to many dictionary (the lambda function is the key selector function). You can see this later in the select where you are using `t.Key...` in the select – RobH Oct 30 '12 at 10:24

2 Answers2

1

Source items (which I assume has type Coverage) are grouped by this key:

new 
{
  c.CoverageType.Code, 
  c.CoverageType.Mnemonic, 
  c.CoverageType.Description, 
  c.CoverageType.CoverageLevel, 
  c.CoverageType.CoveragePosition
}

For each distinct key in the input sequence a CoverageOption is created. Note that a single key may be associated with multiple Coverage items and for each key and one or more associated Coverage items the following properties of CoverageOption is computed:

  • The CoverageType property is computed from the key.

  • The Limits property is computed by taking all the Limit values of the associated Coverage items and for each distinct Coverage.Limit.Code pick the first Limit instance. So this is a sequence of Limit objects each having a distinct Code.

  • The Deductibles property is computed by taking all the Deductible values of the associated Coverage items and for each distinct Coverage.Deductible.Code pick the first Deductible instance. So this is a sequence of Deductible objects each having a distinct Code.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • Wow, this answer is so awesome. I am going through it, i am sure this would clear the clouds!! Thanks a lot man! – Charu Oct 30 '12 at 11:00
  • so the key for this ToLookup was a Tuple initially which i changed to a anonymous type as in the code above. Can you comment on this, if what i did was right? I removed tuples because i had to access the keys using items1, 2 etc which made the code unreadable. – Charu Oct 30 '12 at 11:03
  • 1
    @Charu: Instead of using an anonymous type as the key you should be able to use `CoverageType`. However, to do that you need to override `CoverageType.Equals` and `CoverageType.GetHashCode` to ensure that different instances with same property values are considered equal. – Martin Liversage Oct 30 '12 at 11:15
0

As documentation states:

The ToLookup(IEnumerable, Func) method returns a Lookup, a one-to-many dictionary that maps keys to collections of values. A Lookup differs from a Dictionary, which performs a one-to-one mapping of keys to single values.

so it is similar to GroupBy but there are some differences that are described here.

Community
  • 1
  • 1
Rafal
  • 12,391
  • 32
  • 54