DataTable.Select and Performance Issue in C#

后端 未结 6 2093
误落风尘
误落风尘 2020-12-15 01:56

I\'m importing the data from three Tab delimited files in the DataTables and after that I need to go thru every row of master table and find all the rows in two child tables

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-15 02:38

    You can speed it up a lot by using a dictionary. For example:

    if (distinctdt.Rows.Count > 0)
    {
        // build index of C1 values to speed inner loop
        Dictionary masterIndex = new Dictionary();
        foreach (DataRow row in rawMasterdt.Rows)
            masterIndex[row["C1"].ToString()] = row;
    
        int count = 0;
        foreach (DataRow offer in distinctdt.Rows)
        {
    

    Then in place of

        string exp = "C1 = " + "'" + offer[0].ToString() + "'" + "";
        DataRow masterRow = rawMasterdt.Select(exp)[0];
    

    You would do this

    DataRow masterRow;
    if (masterIndex.ContainsKey(offer[0].ToString())
        masterRow = masterIndex[offer[0].ToString()];
    else
        masterRow = null;
    

提交回复
热议问题