Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Creating a list of complex object from two lists [modified]

Creating a list of complex object from two lists [modified]

Scheduled Pinned Locked Moved C#
csharplinqtutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dan sh
    wrote on last edited by
    #1

    Assume there are two classes: Zoo and Animals. Something like this:

    public class Zoo{
    public int Id;
    public string Name;
    public List< Animals > animals;
    }

    public class Animals{
    public int Id;
    public string Name;
    }

    Now, I have two generic lists: 1. List of Zoo 2. List of Animals 3. Any kind of collection (I cannot decide which one to use) which will have Zoo.Id and Animal.Id. This collection would be used as a map between Zoo and Animals. Here each Zoo will have multiple animals and same animal can be a part of multiple zoos. I need to get the list of Animals in a Zoo and then set that value to Zoo.animals list. I am thinking to use LINQ. Something like:

    foreach(Zoo zoo in zooList){
    zoo.animals = (from animal in animalList
    where collectionThree[animal.Id] = zoo.Id
    select animal).ToList();
    }

    But since collectionThree cannot be a Dictionary/HashTable, this wont work. Any ideas how to do this?

    modified on Wednesday, March 10, 2010 10:56 PM

    M D K 3 Replies Last reply
    0
    • D dan sh

      Assume there are two classes: Zoo and Animals. Something like this:

      public class Zoo{
      public int Id;
      public string Name;
      public List< Animals > animals;
      }

      public class Animals{
      public int Id;
      public string Name;
      }

      Now, I have two generic lists: 1. List of Zoo 2. List of Animals 3. Any kind of collection (I cannot decide which one to use) which will have Zoo.Id and Animal.Id. This collection would be used as a map between Zoo and Animals. Here each Zoo will have multiple animals and same animal can be a part of multiple zoos. I need to get the list of Animals in a Zoo and then set that value to Zoo.animals list. I am thinking to use LINQ. Something like:

      foreach(Zoo zoo in zooList){
      zoo.animals = (from animal in animalList
      where collectionThree[animal.Id] = zoo.Id
      select animal).ToList();
      }

      But since collectionThree cannot be a Dictionary/HashTable, this wont work. Any ideas how to do this?

      modified on Wednesday, March 10, 2010 10:56 PM

      M Offline
      M Offline
      masoumeh2010
      wrote on last edited by
      #2

      Union statement in SQL Server can be useful.

      1 Reply Last reply
      0
      • D dan sh

        Assume there are two classes: Zoo and Animals. Something like this:

        public class Zoo{
        public int Id;
        public string Name;
        public List< Animals > animals;
        }

        public class Animals{
        public int Id;
        public string Name;
        }

        Now, I have two generic lists: 1. List of Zoo 2. List of Animals 3. Any kind of collection (I cannot decide which one to use) which will have Zoo.Id and Animal.Id. This collection would be used as a map between Zoo and Animals. Here each Zoo will have multiple animals and same animal can be a part of multiple zoos. I need to get the list of Animals in a Zoo and then set that value to Zoo.animals list. I am thinking to use LINQ. Something like:

        foreach(Zoo zoo in zooList){
        zoo.animals = (from animal in animalList
        where collectionThree[animal.Id] = zoo.Id
        select animal).ToList();
        }

        But since collectionThree cannot be a Dictionary/HashTable, this wont work. Any ideas how to do this?

        modified on Wednesday, March 10, 2010 10:56 PM

        D Offline
        D Offline
        Dan Mos
        wrote on last edited by
        #3

        Or SortedList. Ex:

        public SortedList<Animal, Zoo> map = new SortedList<Animal, Zoo>();
        ...
        ...
        ...
        //and linq
        zoo.animals = (from animal in animalList
        where map[animal].Id == zoo.Id
        select animal).ToList();
        }

        or

        public SortedList<int, int> map = new SortedList<int, int>();
        ...
        ...
        ...
        //fill the sortedList from the DReader
        //and linq
        zoo.animals = (from animal in animalList
        where map[animal.Id] == zoo.Id
        select animal).ToList();

        modified on Wednesday, March 10, 2010 2:25 PM

        D 1 Reply Last reply
        0
        • D Dan Mos

          Or SortedList. Ex:

          public SortedList<Animal, Zoo> map = new SortedList<Animal, Zoo>();
          ...
          ...
          ...
          //and linq
          zoo.animals = (from animal in animalList
          where map[animal].Id == zoo.Id
          select animal).ToList();
          }

          or

          public SortedList<int, int> map = new SortedList<int, int>();
          ...
          ...
          ...
          //fill the sortedList from the DReader
          //and linq
          zoo.animals = (from animal in animalList
          where map[animal.Id] == zoo.Id
          select animal).ToList();

          modified on Wednesday, March 10, 2010 2:25 PM

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          Thanks! SortedList is what I was looking for.

          1 Reply Last reply
          0
          • D dan sh

            Assume there are two classes: Zoo and Animals. Something like this:

            public class Zoo{
            public int Id;
            public string Name;
            public List< Animals > animals;
            }

            public class Animals{
            public int Id;
            public string Name;
            }

            Now, I have two generic lists: 1. List of Zoo 2. List of Animals 3. Any kind of collection (I cannot decide which one to use) which will have Zoo.Id and Animal.Id. This collection would be used as a map between Zoo and Animals. Here each Zoo will have multiple animals and same animal can be a part of multiple zoos. I need to get the list of Animals in a Zoo and then set that value to Zoo.animals list. I am thinking to use LINQ. Something like:

            foreach(Zoo zoo in zooList){
            zoo.animals = (from animal in animalList
            where collectionThree[animal.Id] = zoo.Id
            select animal).ToList();
            }

            But since collectionThree cannot be a Dictionary/HashTable, this wont work. Any ideas how to do this?

            modified on Wednesday, March 10, 2010 10:56 PM

            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #5

            Your 3rd collection is necessary, in fact it breaks the object encapsulation IMO. What you really need is a List<Zoo> property on Animal, and conversely a List<Animal> property on Zoo. All you need do is ensure that the lists are not accessible publicly, and methods are provided for addition / removal to both list properties "simultaneously" (i.e. adding an Animal to a zoo also adds that zoo to an animal). You will also need properties or methods for getting copies (so the originals are unalterable externally) of the lists.

            Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

            D D 2 Replies Last reply
            0
            • K Keith Barrow

              Your 3rd collection is necessary, in fact it breaks the object encapsulation IMO. What you really need is a List<Zoo> property on Animal, and conversely a List<Animal> property on Zoo. All you need do is ensure that the lists are not accessible publicly, and methods are provided for addition / removal to both list properties "simultaneously" (i.e. adding an Animal to a zoo also adds that zoo to an animal). You will also need properties or methods for getting copies (so the originals are unalterable externally) of the lists.

              Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

              D Offline
              D Offline
              Dan Mos
              wrote on last edited by
              #6

              :thumbsup: Or make them totally unaware of each other as in:

              class Animal{
              public int Id{get;set;}
              public string Name{get;set;}
              }

              class Zoo{
              public int Id{get;set;}
              public string Name{get;set;}
              }

              //and a sortedList or a class that represnts the AnimalZoo jonction table, but why bother to implement
              //IComparable, IEqutable ... when you already have it.
              SortedList<int,int> animalZooJonction = new SortedList<int, int>();
              //the animalI.Id is key and zoo.Id is value

              //Now when he wants to get the animals for a given zoo:

              var animalsInSpecifiedZoo = (from rec in animals
              where animalZooJonction[rec.Id] == zoo//zoo specified as a param
              select rec).ToList();

              While it is not as keen on respecting standars it is faster and it does consume less memory.

              1 Reply Last reply
              0
              • K Keith Barrow

                Your 3rd collection is necessary, in fact it breaks the object encapsulation IMO. What you really need is a List<Zoo> property on Animal, and conversely a List<Animal> property on Zoo. All you need do is ensure that the lists are not accessible publicly, and methods are provided for addition / removal to both list properties "simultaneously" (i.e. adding an Animal to a zoo also adds that zoo to an animal). You will also need properties or methods for getting copies (so the originals are unalterable externally) of the lists.

                Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                D Offline
                D Offline
                dan sh
                wrote on last edited by
                #7

                Thanks for the reply. :) I have tried to make actual scenario simpler with that example. The actual code is a bit different. What you have said is there in the actual code as well except for having lists in both the classes.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups