when i want to select data from the DB I’m doing something like this
string query = String.Format(@"....); List<List<object>> Details = DBUtils.selectDataFromServer(query); if (blilDetails [0].Any()) { List<string> mixer = Details [0].Select(Convert.ToString).ToList();// this is the first colums in the data base List<int> prodID = Details [1].Select(Convert.ToInt32).ToList(); //second colum //and so on }
and this is the function
public static List<List<object>> selectDataFromServer(string query) { List<List<object>> columnData = new List<List<object>>(); try { var v = new List<List<object>>(); using (SqlConnection connection = new SqlConnection(Settings.Default.connectionString)) { connection.Open(); using (SqlCommand cmd1 = new SqlCommand(query, connection)) { using (SqlDataReader myReader = cmd1.ExecuteReader()) { using (DataTable table = new DataTable()) { table.Load(myReader); string[] columnNames = table.Columns.Cast<DataColumn>() .Select(x => x.ColumnName) .ToArray(); foreach (string columnName in columnNames) { List<object> allRowValues = table.AsEnumerable().Select(r => r[columnName]).ToList(); v.Add(allRowValues); } } } } } return v; } catch (Exception ex) { Console.WriteLine(ex.ToString()); return columnData; } }
and this, to me look tedious, if i have 10 columns i have to do 10 converts to 10 lists i was wondering if there’s a better way to do it