Select to view content in your preferred language

Simple SQL Query, .net

269
4
Jump to solution
07-23-2024 02:17 PM
SP2501
by
Emerging Contributor

This is probably going to be a simple answer, but im having some trouble formatting an SQL query in C#.

I need to query a featuretable and pick out a feature that has a matching "fips". 

the feature name is a string, example shows a number but they can be strings.

When I use the following code, I keep getting a return (featureresult2) of ALL the features in the table. I have tried a few different ways of phrasing the query, but it seems I am missing something.

name = S.UniqueID; 

//Lets look up the geometry
string s = "fips";
string columnName = s;
string searchString = name;
// string sqlQuery = $"{columnName} = {searchString}";//Returning all
string sqlQuery = "fips = '270530265081'"; //Testing simple, still returning all


QueryParameters queryParams = new QueryParameters //Selects the fips
{
WhereClause = sqlQuery
};
FeatureQueryResult featureResult2;
featureResult2 = (BlockGroupGeoPackage.GeoPackageFeatureTables[0].QueryFeaturesAsync(queryParams).GetAwaiter().GetResult());

 

 

 

0 Kudos
1 Solution

Accepted Solutions
JenniferNery
Esri Regular Contributor

I don't see anything wrong with how you formed the query. String equality looks correct provided the field exists with the correct name and type. If you want query to return just one, you can set MaxFeatures = 1; but if you say this value is unique then you should expect a single feature returned without MaxFeatures.

var attributeValue = "270530265081";

var table = BlockGroupGeoPackage.GeoPackageFeatureTables[0];
var tableField = table.GetField("fips");


var query = new QueryParameters()
{
  WhereClause = $"{tableField.Name} = '{attributeValue}'",
  MaxFeatures = 1
};


I would await the result of query though and may be inspect the features that come back. Output window should have the attribute value for each feature.

var features = await table.QueryFeaturesAsync(query);

foreach(var feature in features)
{
  // Consider printing other attributes to identify the feature (i.e. OBJECTID)
  System.Diagnostics.Debug.WriteLine($"{tableField.Name}: feature.Attribute[tableField.Name]")
}

 

View solution in original post

0 Kudos
4 Replies
JenniferNery
Esri Regular Contributor

I don't see anything wrong with how you formed the query. String equality looks correct provided the field exists with the correct name and type. If you want query to return just one, you can set MaxFeatures = 1; but if you say this value is unique then you should expect a single feature returned without MaxFeatures.

var attributeValue = "270530265081";

var table = BlockGroupGeoPackage.GeoPackageFeatureTables[0];
var tableField = table.GetField("fips");


var query = new QueryParameters()
{
  WhereClause = $"{tableField.Name} = '{attributeValue}'",
  MaxFeatures = 1
};


I would await the result of query though and may be inspect the features that come back. Output window should have the attribute value for each feature.

var features = await table.QueryFeaturesAsync(query);

foreach(var feature in features)
{
  // Consider printing other attributes to identify the feature (i.e. OBJECTID)
  System.Diagnostics.Debug.WriteLine($"{tableField.Name}: feature.Attribute[tableField.Name]")
}

 

0 Kudos
SP2501
by
Emerging Contributor

Thank you!

Looks like you were correct, and the query was good. The inspect loop you provided would only run once even when I removed the max results of the query. 

Turns out this was in issue with Visual Studios watch command. Watching the NumberOfFeatures of the query results(features), seems to be a count of the original table being queried.

 

Thanks for the help!

0 Kudos
JenniferNery
Esri Regular Contributor

No problem.

The NumberOfFeatures would be a cumulative value of the features retrieved in the table so far. So if you have this table rendered as a layer on a map, a query with view extent would have already run to fetch features. Or if you had previous queries made with different fips value.


There is a QueryFeatureCountAsync method if you only want the count for features from a specific query. But if you will be working on the features returned from QueryFeaturesAsync then you can use features.Count() instead where features is the return value of the awaited query task.

dotMorten_esri
Esri Notable Contributor

On an un-related note, try and avoid blocking code like 'GetAwaiter().GetResult());' but instead use async/await for all async methods.

There's a very large chance that you could deadlock the application without it.