0

I need to query a table from database which has 400 rows and 24 columns. I need to query this table so that on each row and then on each column of row I can perform some C# code ( I can use column information to execute some code).

Now at the moment I am querying each row again and again from table using select statement and storing to a custom list and performing custom operations on it.

Is it best and fastest way of doing it ? or should I just query the whole table one time and store somewhere ? not sure where in a dataset and then run throw custom code to do some operation using information in each row ?

3
  • Do you always need the whole table data? Commented Jul 8, 2013 at 10:30
  • yup and it's one time application that i need to run on live server Commented Jul 8, 2013 at 10:30
  • It's always best to export your data to csv or some file. Then using a loop you can iterate through each row of your file then you can assign each element of a row to an arrary.. Like this you can process.. Just a suggestion.. Commented Jul 8, 2013 at 10:34

6 Answers 6

2

You can fetch the table from database once and store it in datatable and then just use linq to select the column something like this

var data = dt.AsEnumerable().Select(s => s.Field<string>("myColumnName")).ToArray<string>();

and If you don't want to use other columns anywhere in your code then you should select only useful column from the database.

You can also select multiple columns of a database using linq. The values will be stored in anonymous type of object.

var mutipleData = from row
                  in dt.AsEnumerable()
                  select new 
                  { Value1 = row["Column1"].ToString(), 
                    Value2 = row["Column2"].ToString() 
                  };
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming that each field is 1000 bytes, the total memory to hold your 400 rows would be 9.6MB. Peanuts! Just read the whole table in a DataTable and process it as you wish.

Comments

1
  • Select all the records from DB table which are f your concern
  • Copy the select records into DataTable.

Pseudo Code:

--dt is datatable
foreach(datarow dr in dt.rows)
{
--perform operation
string str=dr["columnname"].tostring 
}

Comments

0

400 rows isn't a massive amount, however it depends on the data in each column and how often you are likely to run the query. If all you are going to do is run the query and manipulate the output, use a DataReader instead.

Comments

0

If it's only 400 records, I'd fetch them all at once, store them in a class and iterate over each instance.

Something like:

Class

public class MyTableClass(){
    string property1 {get; set;}
    string property2 {get; set;}
    int property3 {get; set;}
    // etc.
}

Logic:

ICollection<MyTableClass> lstMyTableClass = (
                from m in db.mytableclass
                select m
            ).ToList();
            return lstMyTableClass;

And then a loop:

 foreach(var myTableInstance in lstMyTableClass){

      myTableInstance.DoMyStuff();   
 }

Comments

0

If the number of records will always be below thoussand, just query all the records and keep it in a List.

Once the data is in the List you can query the List n number of times using LINQ without hitting the database for each request.

2 Comments

this is what i am doing at the moment :)
Are you calculating something based on the values in each column?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.