Return account string that not found in current layer?

333
2
Jump to solution
06-16-2020 01:32 PM
helenchu
Occasional Contributor II

All,

I have the codes below to do selection but I also need the code to return to me what account in the 'queryString' that it could not find in my layer.  How do I do that?  Thank you!

queryString = accountNum in ('123', '456', '789')

public static Task<int> SelectByAttributeAsync(string featureLayerName, string queryString)
        {
            return QueuedTask.Run(async () =>
            {
                var firstLyr =
                    MapView.Active.Map.FindLayers(featureLayerName).FirstOrDefault() as FeatureLayer;
                if (firstLyr == null) throw new Exception(string.Format("The feature class: {0} does not exist", featureLayerName));
                var qf = new QueryFilter()
                {
                    WhereClause = queryString,
                    SubFields = "*"
                };
            };              
             
                MapView.Active.ZoomToSelected();
                MapView.Active.ZoomToSelected(new TimeSpan(0, 0, 3), true);
         return selectionCount;
                
        });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

You can use the snippet below to get this result when searching for these accounts (type string) ' "Test 0", "Test 1", "Test x"' - Note that only Test x doesn't exist:

In essence you first do your selection and then search through the selected set one by one to find the 'missing' item.

 protected override async void OnClick()
    {
      Tuple<int, string> selectionInfo = null;
      try
      {
        var accounts = new List<string>() { "Test 0", "Test 1", "Test x" };

        selectionInfo = await SelectByAttributeAsync("TestPoints", accounts);
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.ToString());
      }
      _ = MapView.Active.ZoomToSelectedAsync(new TimeSpan(0, 0, 3), true);
      MessageBox.Show($@"{selectionInfo.Item1} records were selected - {selectionInfo.Item2}");
    }

    public static Task<Tuple<int, string>> SelectByAttributeAsync(string featureLayerName, List<string> accounts)
    {
      return QueuedTask.Run(() =>
      {
        var firstLyr = MapView.Active.Map.FindLayers(featureLayerName).FirstOrDefault() as FeatureLayer;
        if (firstLyr == null) throw new Exception(string.Format("The feature class: {0} does not exist", featureLayerName));
        // make list of accounts into 'in' clause
        var commaList = string.Join(",", accounts.Select(p => $@"'{p}'"));
        var qf = new QueryFilter()
        {
          WhereClause = $@"description in ({commaList})",
          SubFields = "*"
        };
        var selection = firstLyr.Select(qf);
        // remainingAccounts tracks first all accounts and each found account will be removed
        var remainingAccounts = new List<string>(accounts);
        using (var foundCursor = firstLyr.Search(new QueryFilter() { ObjectIDs = selection.GetObjectIDs() })) 
        {
          while (foundCursor.MoveNext())
          {
            using (var row = foundCursor.Current)
            {
              var foundAccount = row["description"].ToString();
              remainingAccounts.Remove(foundAccount);
            }
          }
        }
        var msg = string.Empty;
        if (remainingAccounts.Count == 0) msg = "All records found";
        else msg = $@"Not found: {string.Join (",", remainingAccounts)}";
        return new Tuple<int, string> (selection.GetCount(), msg);
      });
    }

View solution in original post

2 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

You can use the snippet below to get this result when searching for these accounts (type string) ' "Test 0", "Test 1", "Test x"' - Note that only Test x doesn't exist:

In essence you first do your selection and then search through the selected set one by one to find the 'missing' item.

 protected override async void OnClick()
    {
      Tuple<int, string> selectionInfo = null;
      try
      {
        var accounts = new List<string>() { "Test 0", "Test 1", "Test x" };

        selectionInfo = await SelectByAttributeAsync("TestPoints", accounts);
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.ToString());
      }
      _ = MapView.Active.ZoomToSelectedAsync(new TimeSpan(0, 0, 3), true);
      MessageBox.Show($@"{selectionInfo.Item1} records were selected - {selectionInfo.Item2}");
    }

    public static Task<Tuple<int, string>> SelectByAttributeAsync(string featureLayerName, List<string> accounts)
    {
      return QueuedTask.Run(() =>
      {
        var firstLyr = MapView.Active.Map.FindLayers(featureLayerName).FirstOrDefault() as FeatureLayer;
        if (firstLyr == null) throw new Exception(string.Format("The feature class: {0} does not exist", featureLayerName));
        // make list of accounts into 'in' clause
        var commaList = string.Join(",", accounts.Select(p => $@"'{p}'"));
        var qf = new QueryFilter()
        {
          WhereClause = $@"description in ({commaList})",
          SubFields = "*"
        };
        var selection = firstLyr.Select(qf);
        // remainingAccounts tracks first all accounts and each found account will be removed
        var remainingAccounts = new List<string>(accounts);
        using (var foundCursor = firstLyr.Search(new QueryFilter() { ObjectIDs = selection.GetObjectIDs() })) 
        {
          while (foundCursor.MoveNext())
          {
            using (var row = foundCursor.Current)
            {
              var foundAccount = row["description"].ToString();
              remainingAccounts.Remove(foundAccount);
            }
          }
        }
        var msg = string.Empty;
        if (remainingAccounts.Count == 0) msg = "All records found";
        else msg = $@"Not found: {string.Join (",", remainingAccounts)}";
        return new Tuple<int, string> (selection.GetCount(), msg);
      });
    }
helenchu
Occasional Contributor II

I've successfully got it worked with my codes.  Thank you so very much.  You're truly life saver!  

0 Kudos