Search code examples
c#silverlightwcf-ria-servicesbing-maps

nested foreach loops and WCF RIA domain service


I am attempting to load a table of shape coordinates out of a database for plotting on a silverlight bing map, The table structure I am interested in is here http://dl.dropbox.com/u/10440538/ADO.png.

Using a wcf ria service I can load the data ok, but I need help with my code to pull out the lowest level data (the coordinates) and separate them into collections. The end result should be so that I have a collection of LocationCollections, one LocationCollection for each mapshape in the database.

This is my loadoperation callback

    void FarmsLoaded(LoadOperation<Farm> loadOp) {

        LocationCollection lc = new LocationCollection();

        foreach (Farm f in loadOp.Entities) {
            foreach (FarmLocation fl in f.FarmLocations) {
                foreach (MapShape ms in fl.MapShapes) {
                    lc.Clear();
                    foreach (MapPoint mp in ms.MapPoints) {
                        lc.Add(new Location(mp.Latitude, mp.Longitude));
                    }
                    shapeList.Add(lc);   //observablecollection of LocationCollections
                }
            }
        }
    }

shapeList is then bound to a map layer. Unfortunately when I run this only the last mapshape in the db is drawn. I think I misunderstand how the foreach nesting is being traversed so I'd appreciate any effort at enlightenment on correct use of nested foreach in this situation, or any alternate suggestions if foreach is not appropriate (LINQ?)


Solution

  • I think the problem is not related to foreach loop, but instead clearing of lc. Since you are claring lc on for each mapshape, they seem to be deleted from shapelist as well.