I have a class Workers as below
Now reader.ExecuteReader() returns a List<Worker> , and as you can see in the line commented out above, I initially thought I could explicitly cast the List<Worker> as a Workers object.
I can see why I am unable to cast List<Worker> to Workers (one can cast a Workers object to a List<Worker> but not the other way round, much like (Fruit)Apple is valid but (Apple)Fruit isn't, in the case of class Apple:Fruit{})
In order to get round this I've had to loop through each of the Worker objects in the List<> and add them to a Workers object, and then return the Workers object.
While this works fine (presumably adding a bit of an overhead), is there a more elegant way I could accomplish this? I'm stumped.
Many thanks for any pointers
~LFCfan
Code:
public class Workers : List<Worker>
{
public static Workers GetAll()
{
WorkersReader reader = new WorkersReader();
-- return (Workers)reader.ExecuteReader();
List<Worker> workerlist = reader.ExecuteReader();
Workers workers = new Workers();
foreach (Worker w in workerlist)
{
workers.Add(w);
}
return workers;
}
}
I can see why I am unable to cast List<Worker> to Workers (one can cast a Workers object to a List<Worker> but not the other way round, much like (Fruit)Apple is valid but (Apple)Fruit isn't, in the case of class Apple:Fruit{})
In order to get round this I've had to loop through each of the Worker objects in the List<> and add them to a Workers object, and then return the Workers object.
While this works fine (presumably adding a bit of an overhead), is there a more elegant way I could accomplish this? I'm stumped.
Many thanks for any pointers
~LFCfan