Take the 2-minute tour ×

I am quering a database and am wondering if there is a way to load the results back into a list. The only way i curerntly know of how to get info from a database is within a datatable. I can always get it into a datatable and then loop through the rows in the datatable and add them to a list. But is there a way to directly get the result of a query into a list? This is my code:

OleDbCommand command= new OleDbCommand("SELECT ID, GRADE, PHONE from database WHERE PHONE=123456", conn);

The result should be a list entry of the form (id, grade, phone):



share|improve this question
What if the query returns more than 1 row? –  Ash Burlaczenko Jun 27 '11 at 14:27
AFIK, you need to call the Add() method of list to add each element. –  Rahul Jun 27 '11 at 14:30
It will only return one row. Thats a requirement –  Ryan Jun 27 '11 at 14:34
why do you need a list if it returns one row? am I missing something? –  Massimiliano Peluso Jun 27 '11 at 14:36

3 Answers 3

up vote 2 down vote accepted

Consider you have a class

public ListData()
  public int Id{get;set;} //Assuming DB field is int
  public string Grade{get;set;}  //Assuming DB field is a string
  public string PhoneNo{get;set;} //Assuming DB field is a string

Then you can do the following:

list<ListData> theList =(from item in dt.AsEnumerable()
                         Select new ListData()
                                 Id= (int)item["ID"],
                                 Grade = (string)item["GRADE"],
                                 PhoneNo = (string)item["PHONE"]


1. I just type it on the comment textbox so there might be some errors if you just copy-paste the code

2. You also need to handle null values if any on GRADE / PHONE

share|improve this answer

Have a look on this question.

share|improve this answer

You can use Table Adapter, e.g.

DataSet mSet = new DataSet();
OleDbTableAdapter mAdapter = new OleDbTableAdapter(myOleDbCommand); 
share|improve this answer

Your Answer


