C# 不使用c中的for循环获取单词表的单元格索引的最佳方法是什么?

C# 不使用c中的for循环获取单词表的单元格索引的最佳方法是什么?,c#,for-loop,ms-word,xceed,C#,For Loop,Ms Word,Xceed,我使用for循环得到单词表单元格的索引,这对较大的表来说需要很多时间,没有for循环有什么方法可以做到这一点吗 public static int[] GetColumnIndex(Xceed.Words.NET.Table table, string columnName, int endRow,int k) { int[] data = { -1, -1 }; for (int j = k; j < endRow; j++

我使用for循环得到单词表单元格的索引,这对较大的表来说需要很多时间,没有for循环有什么方法可以做到这一点吗

public static int[] GetColumnIndex(Xceed.Words.NET.Table table, string columnName, int endRow,int k)
        {
            int[] data = { -1, -1 };
            for (int j = k; j < endRow; j++)
            {
                for (int i = 0; i < table.Rows[j].Cells.Count; ++i)
                {
                    if (table.Rows[j].Cells[i].Paragraphs[0].Text.Equals("«" + columnName + "»"))
                    {
                        data[0] = j;
                        data[1] = i;
                        return data;
                    }
                }
            }

            return data;
        }

根据访问模式优化算法可以做的几件事是,搜索同一个表的次数事实上,由于在表中搜索列名,搜索次数会随着表变大而快速增加。因此,有必要将表中的数据转换为一个数据结构,该结构由例如a的单词索引

首先,创建一个包含表内容的类。这样,当您要搜索同一个表时,可以使用该类的同一实例,并避免基于排序字典重新创建数据结构:

public class XceedTableAdapter
{
   private readonly SortedDictionary<string, (int row, int column)> dict;

   public XceedTableAdapter(Xceed.Words.NET.Table table)
   {
      this.dict = new SortedDictionary<string, (int, int)>();
      // Copy the content of the table into the dict.
      // If you have duplicate words you need a SortedDictionary<string, List<(int, int)>> type. This is not clear in your question.
      for (var i = 0, i < rowCount; i++)
      {
          for (var j = 0; j < columnCount; j++)
          {
              // this will overwrite the index if the text was previously found: 
              this.dict[table.Rows[i].Cells[j].Paragraphs[0].Text] = (i, j);
          }
      }
   }

   public (int, int) GetColumnIndex(string searchText)
   {
      if(this.dict.TryGetValue(searchText, out var index))
      {
          return index;
      }

      return (-1, -1);
   }
}

我有点困惑。首先,我对XCeed不熟悉,所以我可能遗漏了一些东西。但是,您似乎有一个具有列名的DataTable和一个名为GetColumnIndex的函数,您正在向该函数传递来自DataTable的列名。您可能希望它返回具有相同列名的列的索引,但是,您正在查看整个表以查找与列标题具有相同内容的单元格。对我来说没有意义。其次,您正在遍历所有列并为datatable中的每一列调用此函数-唯一有效的调用是最后一个,因为您每次都覆盖ind变量。您可以尝试并明确您试图实现的内容,并使上述代码有意义,然后有人可能会回答您的问题。我正在尝试使用datatable填充word表。首先,我在word表中搜索列标题,该标题在datatable中也可用,然后我将该列的所有行从datatable填充到word表中。foreach循环中有更多代码将数据填充到表中,但我没有显示它,因为不需要。。。我只想用最快的方法获取单元索引或任何其他方法…从而复制内存中的整个表,并将所有cpu负载转移到创建XceedTableAdapter的几行位置-提到了大型表。@TimRutter这取决于您使用原始表的方式。在我的解决方案中,我没有保留对原始表的引用,因此无法及时对其进行gc编辑。此外,即使你不得不,出于某种原因,你也必须选择付出代价。按行和列索引的未排序表不足以进行频繁搜索。我想,如果有大量调用GetColumnIndex,将减少那里的工作负载。
public class XceedTableAdapter
{
   private readonly SortedDictionary<string, (int row, int column)> dict;

   public XceedTableAdapter(Xceed.Words.NET.Table table)
   {
      this.dict = new SortedDictionary<string, (int, int)>();
      // Copy the content of the table into the dict.
      // If you have duplicate words you need a SortedDictionary<string, List<(int, int)>> type. This is not clear in your question.
      for (var i = 0, i < rowCount; i++)
      {
          for (var j = 0; j < columnCount; j++)
          {
              // this will overwrite the index if the text was previously found: 
              this.dict[table.Rows[i].Cells[j].Paragraphs[0].Text] = (i, j);
          }
      }
   }

   public (int, int) GetColumnIndex(string searchText)
   {
      if(this.dict.TryGetValue(searchText, out var index))
      {
          return index;
      }

      return (-1, -1);
   }
}
var searchableTable = new XceedTableAdapter(doc.Tables[0]);

foreach (var col in dt.Columns)
{
   ind = searchableTable.GetColumnIndex(col);
}