Concatenate query string in a Loop?

3227
3
Jump to solution
02-04-2014 07:08 AM
ionarawilson1
Occasional Contributor III
I need to add a OR expression to a query depending on the values in a loop. So for example if I have three related records , how can I create two OR expressions to add to the definition expression, which each attribute value added to the OR expression? Thanks

It would be something like this, with each objectidstring being a different object id:

var defexpr:String =  "OBJECTID" +  " like " +  "'"  +  objectidstring + "'" " OR " + "OBJECTID" +  " like " +  "'"  +  objectidstring + "'" " OR " + "OBJECTID" +  " like " +  "'"  +  objectidstring + "'"



     function onResult(relatedRecords:Object, token:Object = null):void      {        // get related records for the first feature       var fset:FeatureSet = (relatedRecords[event.features[0].attributes.OBJECTID]);       if (fset is FeatureSet)       {           relatedRecordsCount = fset.attributes.length;        numberofrelatedrecords.text = relatedRecordsCount.toString()        for (var i=0, il=fset.attributes.length; i<il; i++)        {           var objectidstring:String = String(fset.attributes.OBJECTID)            Alert.show(objectidstring)                }               var defexpr:String =  "OBJECTID" +  " like " +  "'"  +  objectidstring + "'";        var defexpr2:String =  " OR " + "OBJECTID" +  " like " +  "'"  +  objectidstring + "'"        yourTable.definitionExpression = defexpr               Alert.show(defexpr)        query.where = defexpr;        query.objectIds=[];        query.returnGeometry = false;        queryTask.executeForIds(query);
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
AnthonyGiles
Frequent Contributor
Ionara,

You need to do a check on the first line so it doesn't get an or added to the front, something like:

var defexpr:String = ""
   for (var i=0, il=fset.attributes.length; i<il; i++)
   {
      if (i=0)
      {
          defexpr =  "OBJECTID" +  " =" +  fset.attributes.OBJECTID
      }
      else
      {
          defexpr = defexpr + " or " + "OBJECTID" +  " = "  +  fset.attributes.OBJECTID
       }
}
     
Regards

Anthony

View solution in original post

3 Replies
BjornSvensson
Esri Regular Contributor
If your OBJECTID field is not a string field, you probably need to drop the quotes  around the values. 

Not really a direct answer to your question, but assuming your OBJECTID field is not a string field you probably should use "=" instead of "LIKE".

And if you're ending up with multiple OR based on the same field, you're usually even better of changing it into an "IN" statement, for example:
OBJECTID in (1,2,3,10,11,101)
0 Kudos
AnthonyGiles
Frequent Contributor
Ionara,

You need to do a check on the first line so it doesn't get an or added to the front, something like:

var defexpr:String = ""
   for (var i=0, il=fset.attributes.length; i<il; i++)
   {
      if (i=0)
      {
          defexpr =  "OBJECTID" +  " =" +  fset.attributes.OBJECTID
      }
      else
      {
          defexpr = defexpr + " or " + "OBJECTID" +  " = "  +  fset.attributes.OBJECTID
       }
}
     
Regards

Anthony
ionarawilson1
Occasional Contributor III
Thank you all for your help! Anthony's solution worked for me:

Thank you so much Anthony!!!
 function onResult(relatedRecords:Object, token:Object = null):void
     { 
      
      var defexpr:String = ""
      // get related records for the first feature
      var fset:FeatureSet = (relatedRecords[event.features[0].attributes.OBJECTID]);
      if (fset is FeatureSet)
      {
  
       relatedRecordsCount = fset.attributes.length;
       numberofrelatedrecords.text = relatedRecordsCount.toString()
       for (var i=0, il=fset.attributes.length; i<il; i++)
       {
          //var objectidstring:String = String(fset.attributes.OBJECTID)
           //Alert.show(objectidstring)
        
        if (i == 0)
        {
         defexpr = "OBJECTID" + " =" + fset.attributes.OBJECTID
        }
        else
        {
         defexpr = defexpr + " or " + "OBJECTID" + " = " + fset.attributes.OBJECTID
        }
       
       }

       yourTable.definitionExpression = defexpr
       Alert.show(defexpr)
       query.where = defexpr;
       query.objectIds=[];
       query.returnGeometry = false;
       queryTask.executeForIds(query);
       
       myAttributeTable.featureLayer = myFeatureLayer  
       myAttributeTable.featureLayer = yourTable
       myAttributeTable.visible = true;
       
       yourTable.visible = true

       myAttributeTable.featureLayer = yourTable
      
     
        
      }

0 Kudos