views:

133

answers:

1

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

A: 

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.

Joe