none
Merging Two Observable Collections RRS feed

  • Question

  • I have two observable collections containing objects defined by these two models, i want to generate a third collection using linq that is an observable collection of continents where the correct countrys have been added, to each of the continents country lists.

    I can do it this way but i would like a cleaner approach

                foreach( ContinentModel ContinentModel in iCountryImplementation.ContinentModels)
                {
                    ContinentModel.Countrys = (from Country in this.CountryModels
                                              where Country.continent_ObjectID == ContinentModel.objectID
                                              select Country).ToList();
                }

    Thanks

    Madaxe

        public class ContinentModel
        {
            public int objectID { get; set; }
            public string continentName { get; set; }
            public List<CountryModel> Countrys {get;set;}
        }
    
        public class CountryModel
        {
            public int objectID { get; set; }
            public int continent_ObjectID { get; set; }
            public string countryName { get; set; }
            public string countryAbbreviation { get; set; }
        }


    As Busy As A Bricky In Beirut




    • Edited by Madaxe Saturday, March 21, 2020 9:43 PM
    Saturday, March 21, 2020 9:34 PM

All replies

  • Hi Madaxe,
    Based on your code, I can't reproduce the situation. Could you provide your two observable collections?
    >>i want to generate a third collection using linq that is an observable collection of continents where the correct countrys have been added
    Could you explain this statement in detail?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 23, 2020 9:18 AM
  • I don't know if this will help, Madaxe, but you might want to take a look at my blog post:

    https://geek-goddess-bonnie.blogspot.com/2020/01/merge-tables-and-sum-columns.html

    In that post, I'm doing this with DataTables, but perhaps some of the LINQ code will work as well for observable collections. I haven't tried it, but take a look, maybe it will help you.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, March 25, 2020 3:26 PM