Loading a generic collection from a SqlReader?

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Hi, is there any underlying difference between loading a generic collection
from a reader using the two methods:

List<User> colUsers = new List<Users>();

while (reader.Read())
{
User user = new User();
user.FirstName = (string)reader["FirstName"];
user.LastName = (string)reader["LastName"];
colUsers .Add(user);
}

----vs----

while (reader.Read())
{
colUsers.Add(new User
(
(string)reader["FirstName"],
(string)reader["LastName"]
));
}


Creating the object first and then adding values worked fine. But using the
second method by loading the values in the constructor, the colUsers.Count
was correct based on the query, but each of the object's properties in the
collection were null. it's as if the object's weren't retaining their values
or state when added to the collection.
 
Back
Top