如何验证 C# VSTO 3 中的 word 文档中是否存在具有给定 ID 的表
我想检查 C# (VS 2008) Visual Studio Tools for Office(版本 3)中的 Word 文档中是否存在具有给定 ID 的表。
显然我可以遍历文档的 Tables 集合并检查每个 ID,但这似乎效率低下; 在我完成后,该文档最终将有几十个表,虽然我知道这不是很多,但循环遍历该集合似乎很草率。 Tables 集合仅按整数 id 进行索引,而不是按分配给表的字符串 ID 进行索引,因此我不能只使用索引,并且文档或表集合没有明显的 Exists 方法。
我想过使用 AsQueryable() 将 Tables 集合转换为 IQueryable,但我不知道如何以可以通过 ID 查询它的方式执行此操作。
指向文档或示例代码的指针将不胜感激,或者如果有更好的方法来实现它,我也完全赞成
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).
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.
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有更好的方法可以做到这一点。 包括 IQueryable 在内的任何解决方案可能都需要在内部迭代集合,因此速度不会更快。
无论如何,性能不太可能成为问题,所以我不会担心效率低下。
如果您经常这样做,您可以提供一个包装器,它迭代一次表并生成您随后使用的字典。
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.
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.