Creating a list of complex object from two lists [modified]
-
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
-
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
Union statement in SQL Server can be useful.
-
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
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
-
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
-
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
Your 3rd collection is necessary, in fact it breaks the object encapsulation IMO. What you really need is a
List<Zoo>
property onAnimal
, and conversely aList<Animal>
property onZoo
. 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.
-
Your 3rd collection is necessary, in fact it breaks the object encapsulation IMO. What you really need is a
List<Zoo>
property onAnimal
, and conversely aList<Animal>
property onZoo
. 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.
: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.
-
Your 3rd collection is necessary, in fact it breaks the object encapsulation IMO. What you really need is a
List<Zoo>
property onAnimal
, and conversely aList<Animal>
property onZoo
. 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.