如何在C#VSTO 3中的单词doc中验证具有给定ID的表的存在

时间:2021-12-24 02:18:29

I want to check for the existence of a table with a given ID in a word document in C# (VS 2008) Visual Studio Tools for Office (version 3).

我想在C#(VS 2008)Visual Studio Tools for Office(版本3)中检查word文档中是否存在具有给定ID的表。

Obviously I can iterate through the document's Tables collection and check every ID, but this seems inefficient; the document will end up having a few dozen tables after I'm done with it, and while I know that's not a lot, looping through the collection seems sloppy. The Tables collection is only indexed by integer id, not by the string ID assigned to the table, so I cant just use an index, and there's no apparent Exists method of the document or tables collection.

显然,我可以遍历文档的Tables集合并检查每个ID,但这似乎效率低下;在我完成之后,该文档最终会有几十个表格,虽然我知道这不是很多,但是通过该集合循环似乎很草率。 Tables集合仅由整数id索引,而不是由分配给表的字符串ID索引,因此我不能仅使用索引,并且文档或表集合中没有明显的Exists方法。

I thought of casting the Tables collection to an IQueryable using AsQueryable(), but I don't know how to go about doing this in such a way that I could query it by ID.

我想过使用AsQueryable()将Tables集合强制转换为IQueryable,但我不知道如何通过ID来查询它。

Pointers to docs or sample code would be appreciated, or if there's a better way to go about it, I'm all for that, too

对文档或示例代码的指针将不胜感激,或者如果有更好的方法可以解决它,我也是为了这一点。

1 个解决方案

#1


1  

I don't think there's a better way to do it. Any solution including IQueryable would presumably need to iterate the collection internally so wouldn't be any faster.

我不认为有更好的方法。任何包含IQueryable的解决方案都可能需要在内部迭代集合,因此不会更快。

Performance is unlikely to be a problem anyway, so I wouldn't worry about the inefficiency.

无论如何,性能不太可能是一个问题,所以我不担心效率低下。

If you are doing it a lot, you could provide a wrapper that iterates once through the tables and generates a dictionary that you subsequently use.

如果你正在做很多事情,你可以提供一个包装器,它在表中迭代一次并生成一个随后使用的字典。

#1


1  

I don't think there's a better way to do it. Any solution including IQueryable would presumably need to iterate the collection internally so wouldn't be any faster.

我不认为有更好的方法。任何包含IQueryable的解决方案都可能需要在内部迭代集合,因此不会更快。

Performance is unlikely to be a problem anyway, so I wouldn't worry about the inefficiency.

无论如何,性能不太可能是一个问题,所以我不担心效率低下。

If you are doing it a lot, you could provide a wrapper that iterates once through the tables and generates a dictionary that you subsequently use.

如果你正在做很多事情,你可以提供一个包装器,它在表中迭代一次并生成一个随后使用的字典。