3

I'm writing a quick utility app with out of the box win forms and data sets.

private void timer1_Tick(object sender, EventArgs e)
{
   if(_maxTimestamp == null) return;

   var newRows = commonLogTableAdapter.GetDataByTimestamp(_maxTimestamp.Value);
   foreach (var row in newRows.Where(c => !dataSet1.CommonLog.Rows.Contains(c.Id)))
   {
      dataSet1.CommonLog.ImportRow(row);
   }
}

Will the lookup to see if the row already exists have the performance of a hash lookup or could it end up searching the whole collection over and over again?

The documentation for DataRowCollection.Contains Method (Object) doesn't say.

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121

1 Answers1

0

You can build a dictionary that will provide a hash for any column

            DataTable dt = new DataTable();

            Dictionary<string, List<DataRow>> dict = dt.AsEnumerable()
                .GroupBy(x => x.Field<string>("Col A"), y => y)
                .ToDictionary(x => x.Key, y => y.ToList());​

If each key has only one value use this

            Dictionary<string, DataRow> dict = dt.AsEnumerable()
                .GroupBy(x => x.Field<string>("Col A"), y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());​
jdweng
  • 33,250
  • 2
  • 15
  • 20