cancel
Showing results for 
Search instead for 
Did you mean: 

Data from DotCMIS Query only working once.

goebelchase
Champ in-the-making
Champ in-the-making
I am working on an application in C# that pulls the names and paths of all documents containing a certain string, and fills arrays with that data for later use.  And I have noticed that, for some reason, I can only use the query data once.
For context, C# does not allow me to initialize the arrays without providing a length for them, so I run the query once, counting the number of items, then use that number to initialize the arrays.
<blockcode>
//Queries Database for all documents fitting criteria
                IItemEnumerable<IQueryResult> qr = session.Query("SELECT * FROM cmis:document WHERE CONTAINS('cmis:name:itar') OR CONTAINS('itar')", false);

                //Counts the number of elements.
                int x = 0;

                foreach (IQueryResult hit in qr)
                {

                    x++;

                }

                //Creates all the arrays.
                string[] names = new string[x];
                string[] ids = new string[x];
                string[] paths = new string[x];
                string[] stamp = new string[x];
</blockcode>
the problem is, I am now forced to run the query again in order to actually fill the arrays.  If I simply attempt to use the 'qr' IQueryResult object again in another foreach loop, as I do with 'qr1' below
<blockcode>
IItemEnumerable<IQueryResult> qr1 = session.Query("SELECT * FROM cmis:document WHERE CONTAINS('cmis:name:itar') OR CONTAINS('itar')", false);

                int y = 0;
                foreach (IQueryResult hit in qr1)
                {
                    names[y] = (hit["cmis:name"].FirstValue + " ");
                    ids[y] = (hit["cmisSmiley SurprisedbjectId"].FirstValue.ToString());
                    y++;

                }
</blockcode>
I get empty arrays.  I have to re-perform the query as above in order to get usable data.
and if I don't use that initial foreach loop that I used to count the elements, it works the second time.  So it appears that somehow, using the query results in a foreach loop then causes that object to be undeclared or otherwise voided.

Is this a known issue?  Is there any way around this?  It really seems silly to have to run the query every time I need to get the query results, rather than simply having the object persist.
2 REPLIES 2

kaynezhang
World-Class Innovator
World-Class Innovator
You don't need to execute the query a second time,you have several options :
1.You can use TotalNumItems properties of IItemEnumerable to get the number of result set first and then construct string array.
2. Use a collection to save string object and then convert it to an string arry,like following

ArrayList myAL = new ArrayList();
foreach (IQueryResult hit in qr)
{
   myAL.Add(hit["cmis:objectId"].FirstValue.ToString());
}
String[] myArr = (String[]) myAL.ToArray( typeof( string ) );

Thank you, that's very helpful.
I wasn't aware ArrayLists could be used like that.