Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a linq to objects query for a nested collection

I am trying to select text from a collection that is three of four deep.

RootObject has a List<ResourceSet> resourceSets

The resourceSets has a List<Resources> resources

The resources has a List<RouteLeg> routeLegs

The routLegs has a List<ItineraryItem> itineraryItems

The each routeLeg contains and object called ItineraryItem and in that object there is a text property.

I am trying to pull out a list of all the text properties on the routeLeg object. As you can see it is nested pretty deep. I can obviously do this in nested loops..(as shown below) but want something cleaner using Linq to Objects but I am having trouble with the multiple nesting.

  ResourceSet testst = new ResourceSet();
            ResourceSet rs;          
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < _Result.resourceSets.Count; i++)
            {
                rs = _Result.resourceSets[i];


                for (int j = 0; j < rs.resources.Count; i++)
                {

                    Resource rec = rs.resources[j];

                    string test = rec.distanceUnit;

                    for (int k = 0; k < rec.routeLegs.Count; k++)
                    {
                        RouteLeg rl = rec.routeLegs[k];

                        for (int l = 0; l < rl.itineraryItems.Count; l++)
                        {
                            ItineraryItem ii = rl.itineraryItems[l];                           
                            sb.Append(ii.instruction.ToString());
                        }
                    }

                }
            }
like image 619
Daniel Egan Avatar asked Jan 22 '13 19:01

Daniel Egan


People also ask

How do you apply LINQ to an object?

The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable or IEnumerable<T> collection directly, without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML. You can use LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey,TValue>.

What collections can LINQ be used with?

LINQ to Objects offers usage of any LINQ query supporting IEnumerable<T>for accessing in-memory data collections without any need of LINQ provider (API) as in case of LINQ to SQL or LINQ to XML.

What are the uses of LINQ to objects?

In a nutshell, LINQ to Objects provides the developer with the means to conduct queries against an in-memory collection of objects. The techniques used to query against such collections of objects are similar to but simpler than the approaches used to conduct queries against a relational database using SQL statements.


1 Answers

You can use SelectMany to fetch the internal items:

var items = result.resourceSets
                  .SelectMany(rs => rs.resources)
                  .SelectMany(res => res.routeLegs)
                  .SelectMany(rl => rl.itineraryItems)
foreach(var x in items)
    sb.Append(x.instruction.ToString());
like image 75
Reed Copsey Avatar answered Oct 12 '22 22:10

Reed Copsey