More Pertinent Usage

suggest change
public IEnumerable<User> SelectUsers()
{
    // Execute an SQL query on a database.
    using (IDataReader reader = this.Database.ExecuteReader(CommandType.Text, "SELECT Id, Name FROM Users"))
    {
        while (reader.Read())
        {
            int id = reader.GetInt32(0);
            string name = reader.GetString(1);
            yield return new User(id, name);
        }
    }
}

There are other ways of getting an IEnumerable<User> from an SQL database, of course – this just demonstrates that you can use yield to turn anything that has “sequence of elements” semantics into an IEnumerable<T> that someone can iterate over.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents