行C#中具有相同值的DataTable

时间:2016-09-24 03:18:25

标签: c# sqlite foreach datatable

我有一个包含150,000多行的数据表,并且列中有一个名为' number'有一些共享/具有相同数字的行。

对于每个唯一的数字,我希望从其他列的每一行中添加总数,如下所示;

 id     number    weight  other_int
---------------------------------------
 2      12345     45      5 
 3      44556     23      2
 4      12345     335     25
 5      12345     15      455
 ...
 ...
 1523   12345     615     78

ID为2,4,5&的列1523全部共享数字12345,我想得到/或添加到这样的列表中;

 //using foreachrow in datatable get unquie values.. 
 foreach (DataRow rowData in Datatable.Rows)
 {
      //do I add to a temp List<string> here then loop through later using
      foreach
 }

 //create list that will we will loop through inside sqlite transaction for speed
 List<string> insertToSQL = new List<string>();

 //Add to insertToSQL using insertToSQL.Add();

 //for each unique number
 foreach(uniquenumber) {

  //build insert string
  string uniquenumber_string = uniquenumber;
  string uniquenumberCount_string = get number of times uniquenumber appears;
  string uniquenumbertotalWeightColumn_string = get total value of combined weight column;

  string insertString = "INSRET INTO dbTable (number, weight,count) values ('" + uniquenumber_string + "','"+ uniquenumbertotalWeightColumn_string +"', '"+ uniquenumberCount_string +"'")";
  insertToSQL.Add(insertString)

 }  

不确定最好的&amp;最快的方式是它必须是一个快速插入并保持用户长时间...希望不到一分钟

1 个答案:

答案 0 :(得分:0)

找到一个非常快的解决方案......

创建了一个列表对象

List<SQLData> _SQLData = new List<SQLData>();

在SQLite事务中进行处理..

                //Initiate database connection 
                string databaseFile = DatabaseFolderPath + @"\database.sqlite";
                using (var conn = new SQLiteConnection(
                    "Data Source=" + databaseFile + ";"))
                {

                    conn.Open();

                    using (var cmd = new SQLiteCommand(conn))
                    {
                        using (var transaction = conn.BeginTransaction())
                        {



                            #region foreach row
                            foreach (DataRow rowData in Datatable.Rows)
                            {

                                string number = Convert.ToString(rowData["number"]);

                                //Check in List and/or update item
                                var matchingNumber = _SQlData.FirstOrDefault(_stoItem => (_stoItem.Number == number));
                                   if (matchingNumber != null)
                                   {

                                      //Number found in List so update count using +1
                                      int getLinesCount = matchingNumber.Count + 1;

                                       //Update with new amount
                                       matchingNumber.Count = getLinesCount;


                                   }else{

                                      //Create New Item in String based of Number


                                   }

                            }
                            #endregion foreach row


                            #region foreach item in the List
                            foreach (var _item in _SQLData)
                             {

                                cmd.CommandText = "INSERT INTO dbTable (number,count) values ('" + _item.Number + "','" + _item.Count + "')";
                                cmd.ExecuteNonQuery();



                            }
                            #endregion foreach item in the List

                         transaction.Commit();

                        }

                    }

通过一些其他调整,我得到了下面生成的数据库表,这样可以节省我更多的时间来访问这个整体数据,因为我只需要读取(循环1000+行而不是150,000+行(原始数据) ))约为5秒,与30秒以上相比

 id     number    count(combined)  weight(combined)
---------------------------------------
 1      12345     4               1,125 
 2      44556     2               32
 3      12345     8               2,500
 4      12345     1               455
 ...
 ...
 23     33456     9               9,924