QueryTable Error

563
5
12-07-2021 07:17 PM
ThiPham12
New Contributor III

I am trying to use query table to create a list of strings, mukey, associated with specific counties, to use for further geoprocessing with other tables in the same geodatabase. I created a dictionary with the county as the key and mukey as the value to query the list of mukeys. However, I have an error: CS1061 at   "MUResults[county].Add(mukey)" saying there is no accessible extension method to Add. I am wondering what I can do to resolve this problem? Here is my script below:  

using (Geodatabase geodatabase = new Geodatabase(connection))
{
   QueryDef queryDef = new QueryDef
   {
      Tables = "MUPOLYGON.Name",
      SubFields = "MUPOLYGON.OBJECTID, MUPOLYGON.AREASYMBOL, MUPOLYGON.MUSYM,           MUPOLYGON.MUKEY",
    };

QueryTableDescription queryTableDescription = new QueryTableDescription(queryDef)
{
  Name = "MUTable",
  PrimaryKeys = geodatabase.GetSQLSyntax().QualifyColumnName("OBJECTID", "AREASYMBOL"),
};

Table queryTable = geodatabase.OpenQueryTable(queryTableDescription);

using (RowCursor rowCursor = queryTable.Search())
{
  while (rowCursor.MoveNext())
  {
    using (var row = rowCursor.Current)
    {
       var county = Convert.ToString(row["AREASYMBOL"]);
       var mukey = Convert.ToString(row["MUKEY"]);
      if (MUResults.ContainsKey(county))
      MUResults[county].Add(mukey);

....

Thanks for your help!

Tags (1)
0 Kudos
5 Replies
GKmieliauskas
Esri Regular Contributor

There is no declaration of MUResults. But as I understand from your code, your last code line must be like that:

MUResults[county]= mukey;
// or
MUResults.Add(county, mukey);
0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

As @GKmieliauskas  mentioned you forgot to include the declaration for MUResults.  With the correct declaration your code can work:

var MUResults = new Dictionary<string, List<string>>();
MUResults.Add("County1", new List<string>() { "First Item for County 1" });
if (MUResults.ContainsKey("County1")) {
    MUResults["County1"].Add("Second Item for County 1"); 
}
0 Kudos
ThiPham12
New Contributor III

Thanks Wolf! I did add the dictionary declaration, but I didn't declare the value, mukey as a List. 

I changed my dictionary declaration from "var MUResults = new Dictionary<string, <string>>();" to "var MUResults = new Dictionary<string, List<string>>();"

Once I changed the mukey declaration, I have the following error "cannot convert from string to System.Collection.Generic.List<string>." How can I resolve this error?  Thanks for your help!

0 Kudos
KennyPham
New Contributor

Seem like the mukey is a string and not a list as declared in the dictionary. 


@ThiPham12 wrote:

Thanks Wolf! I did add the dictionary declaration, but I didn't declare the value, mukey as a List. 

I changed my dictionary declaration from "var MUResults = new Dictionary<string, <string>>();" to "var MUResults = new Dictionary<string, List<string>>();"

Once I changed the mukey declaration, I have the following error "cannot convert from string to System.Collection.Generic.List<string>." How can I resolve this error?  Thanks for your help!


       var mukey = Convert.ToString(row["MUKEY"]);

 

you have to know if the content in mukey column is a string or you want to have a list of item (so a list of string). I think just change the dictionary to new Dictionary<string, string> should be fine.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I used the community sample data for my code below, specifically the data in C:\Data\Admin\AdminData.gdb.  Needless to say, i had to change the table names and field names.  Here is the code for my sample button 'OnClick':

protected override async void OnClick()
{
  try
  {
    var MUResults = new Dictionary<string, List<string>>();
    var gdbFolder = @"C:\Data\Admin\AdminData.gdb";
    var gdbPath = new FileGeodatabaseConnectionPath(new Uri(gdbFolder));
    await QueuedTask.Run(() =>
    {
      using (Geodatabase geodatabase = new Geodatabase(gdbPath))
      {
        QueryDef queryDef = new QueryDef
        {
          Tables = "PopulationByCounty",
          SubFields = "OBJECTID, NAME, POP"
        };

        QueryTableDescription queryTableDescription = new QueryTableDescription(queryDef)
        {
          Name = "PopulationByCounty",
          PrimaryKeys = geodatabase.GetSQLSyntax().QualifyColumnName("PopulationByCounty", "NAME")
        };

        Table queryTable = geodatabase.OpenQueryTable(queryTableDescription);
        using (RowCursor rowCursor = queryTable.Search())
        {
          while (rowCursor.MoveNext())
          {
            using (var row = rowCursor.Current)
            {
              var county = row["NAME"].ToString();
              var mukey = row["POP"].ToString();
              if (MUResults.ContainsKey(county))
                MUResults[county].Add(mukey);
              else
                MUResults.Add(county, new List<string>() { mukey });
            }
          }
        }
      }
    });
    foreach (var county in MUResults.Keys)
    {
      System.Diagnostics.Trace.WriteLine($@"County: {county}");
      foreach (var mukey in MUResults[county])
      {
        System.Diagnostics.Trace.WriteLine($@" MUKEY - {mukey}");
      }
    }
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}

And here some of the content (as output by my sample) for the dictionary:

County: Autauga County, Alabama
 MUKEY - 54597
 MUKEY - 54773
 MUKEY - 54893
 MUKEY - 55869
County: Baldwin County, Alabama
 MUKEY - 182265
 MUKEY - 183112
 MUKEY - 199183
 MUKEY - 223234
County: Barbour County, Alabama
 MUKEY - 27455
 MUKEY - 27327
 MUKEY - 26755
 MUKEY - 24686
County: Bibb County, Alabama
 MUKEY - 22915
 MUKEY - 22870
 MUKEY - 22553
 MUKEY - 22394

0 Kudos