LINQ To Dictionary

foreach(Dog d in Dogs)
{
      string name = d.Name;
      int age = a.age;
      foreach(Paw p in d.Paws)
      {
             int pawShape = p.Shape;
             foreach(Toe t in p.Toes)
             {
                  int toeStrength = t.Strength;
                  foreach (ToeNail n in t.ToeNails)
                  {        
                        int nailRating = n.Rating;
                        string nailContext = n.Context;
                  }
             }

      }
}

I don't wish to use so many foreach loops. How can I extract the required data using LINQ?

I woul like to produce a list like so:
Name PawId PawShape ToeId TwoStrength NailId NailRating NailContext
Fred 123   3          2      3          3        5        solid
Spot 546   8          4      8          4        3        damaged  

It would also be nice if I could put the data in a dictionary using the Name as the Key?
XPUSRAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

joriszwaenepoelCommented:
I am not a LINQ expert, but I use it on a regular basis.
You code look clean and easy to understand the way it is.

I use link for the following situations:
- filtering data
- sorting data
- joining 2 collections together

However, I don't think you can simplify your code by using LINQ for this kind of situations.

Unless every child-object also has a relation to its parent object.
In that case you first need to create a single collection of all the "toenails", and then you can write

from tn in toenails select new with {.name = tn.Toe.Paw.Dog.Name, .PawId = tn.Toe.Paw.ID, ...}
0
Dennis AriesCEO @ Arkro ITCommented:
On http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx, you will find many examples for Linq.
I think you are looking for something like http://msdn.microsoft.com/en-us/vcsharp/ee908647.aspx
in conjuction with http://msdn.microsoft.com/en-us/vcsharp/aa336748.aspx to convert it to the dictionary.
0
Fernando SotoRetiredCommented:
Hi XPUSR;

This should give you what you want.

Fernando
var result = from d in MyDogs
             from p in d.Paws
             from t in p.Toes
             from n in t.ToeNails
             select new { d.Name, d.Age, p.PawID, p.Shape, n.NailID, n.Rating, n.Context };

foreach( var r in result )
{
    Console.WriteLine(r.Name + " : " + r.Age + " : " + r.PawID + " : " + r.Shape + " : " + r.NailID + " : " + r.Rating + " : " + r.Context);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
XPUSR, is this issue resolved?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.