title: c# dynamic sql looping through server command or linq
body: <p>Given a data set where you will only know the relevant fields at run time. I want to select each row of data for analysis through a loop. Is it better to:</p>
  
  <ol>
  <li><p>run a direct sql query to get the row each time by directly opening and closing the database</p></li>
  <li><p>pull all the applicable rows into a datatable before the loop then selecting them through linq from inside the loop</p></li>
  </ol>
  
  <p>For example, I am reading in a file that says look for rows a b and c then my query becomes "SELECT col1, col2, col3 FROM table WHERE col1 = 'a' or col1 = 'b' or col1= 'c'"
  But I dont know it will be a,b,c during compile time, only after i run the program</p>
  
  <p>Thanks</p>
  
  <p>edit: better in terms or speed and best practice</p>