filter again a QueryFilter Search

782
5
07-11-2017 11:06 AM
Highlighted
New Contributor II

I'm using a QueryFilter.Search to populate a ListBox with the name of all the records. Once all the records are shown, I want to be able to allow a user to filter them according to a string of characters. For this I'm creating a new QueryFilter in which the WhereClause is "FIELD LIKE '%" + NamePart + "%'". However I can't figure out why this new QueryFilter doesn't work even with no WhereClause. Does some one know why? Should the new search be "reset" in some way?

Reply
0 Kudos
5 Replies
Highlighted
New Contributor III

Andres,

Would you be able to provide a snippet of your code (including initial search, and secondary filter attempt)?

Also against which datastore are you executing the search (if it is an enterprise geodatabase, which dbms?)

Thanks,

Colin

Reply
0 Kudos
Highlighted
New Contributor II

Thank you Colin,

First this happens:

        private async Task AddLayerToMap(string layerPath, string layerField, string layerFileName)
        {
            try
            {
                // Get the first map called "Map" from the current project.
                Map myMap = null;
                myMap = await GetMapFromProject(Project.Current, "Map");
                if (myMap == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Failed to get map.");
                    return;
                }
                await QueuedTask.Run(() =>
                {
                    SelectedLayer = MapView.Active.Map.FindLayers(layerFileName, true).FirstOrDefault() as FeatureLayer;
                    if (SelectedLayer == null)
                    {
                        SelectedLayer = LayerFactory.CreateFeatureLayer(new Uri(layerPath), myMap);
                    }
                    MapView.Active.ZoomToAsync(SelectedLayer);
                    if (SelectedLayer == null)
                    {
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Failed to create layer for url:" + layerPath);
                        return;
                    }
                    else
                    {
                        var queryFilter = new QueryFilter();
                        queryFilter.PostfixClause = "ORDER BY " + layerField;
                        var rc = SelectedLayer.Search(queryFilter);
                        FieldList.Clear();
                        while (rc.MoveNext())
                        {
                            using (Feature feature = (Feature)rc.Current)
                            {
                                string name = Convert.ToString(feature[layerField]);
                                FieldList.Add(name);
                            }
                        }
                    }
                });
            }
            catch (Exception exc)
            {
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught: " + exc.Message);
                return;
            }
            FieldListBox.Items.Clear();
            foreach (string ListItem in FieldList) { FieldListBox.Items.Add(ListItem); }
        }

Then this:
      

void NameFilterBox_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox source = e.Source as TextBox;
            NamePart = source.Text;

            QueryFilter queryFilter = new QueryFilter();

            if (NamePart == "")
               { queryFilter.WhereClause = ""; }
            else
               { queryFilter.WhereClause = layerField + " LIKE '%" + NamePart + "%'"; }

            queryFilter.PostfixClause = "ORDER BY " + layerField;

            RowCursor rc = SelectedLayer.Search(queryFilter); // <- returns null !!!

            //Create list of oids to update
            while (rc.MoveNext())
            {
                using (Feature feature = (Feature)rc.Current)
                {
                    string name = Convert.ToString(feature[layerField]);
                    FieldList.Add(name);
                }
            }
        }

Reply
0 Kudos
Highlighted
New Contributor III

Andres,

What happens if you execute the two cursors from the same function?

*pseudo code*

await QueuedTask.Run(() =>
                {
SelectedLayer = MapView.Active.Map.FindLayers(layerFileName, true).FirstOrDefault() as FeatureLayer;

 

                        var queryFilter = new QueryFilter();

                        queryFilter.PostfixClause = "ORDER BY " + layerField;

                       //Query 1

                        var rc = SelectedLayer.Search(queryFilter);

                      //yeah we have fields!

                      queryFilter = new QueryFilter();

                      queryFilter.WhereClause = layerField + " LIKE '%" + NamePart + "%'"; }

                     queryFilter.PostfixClause = "ORDER BY " + layerField;

                     //Query 2

                    RowCursor rc2 = SelectedLayer.Search(queryFilter); // <- returns ?

                     ...

I noticed you were missing a QueuedTask.Run in your second function, if that was true I would have expected to have returned an error (not a null RowCursor), I therefore assumed this was a typo in posting the example.

Colin

Highlighted
New Contributor II

Colin,

Funnily it seems that it was the missing QueuedTask.Run that produced the null RowCursor. I didn't use it because the ProConcepts Geodatabase · Esri/arcgis-pro-sdk Wiki · GitHub page says "you can create a QueryFilter on a non-MCT thread". (a concept I don't fully understand yet).

Many thanks,

Andres

Reply
0 Kudos
Highlighted
New Contributor III

Andres,

The QueryFilter can be created on a non-MCT thread, its the call to Search that needs to be on the MCT thread. The issue we will investigate internally is why you did not receive the expected “CalledOnWrongThreadException” but instead received an null row cursor.

Colin