简体   繁体   English

DataSet主键列是否已编入索引?

[英]Are DataSet primary key columns indexed?

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. DataRowCollection.Contains Method(Object)的文档没有说明。

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());​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM