More Pertinent Usage C# with Example



More Pertinent Usage C# with Example

public IEnumerable 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 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 that 
someone can iterate over. 

0 Comment's

Comment Form

Submit Comment