I will share my code from this process for logic reasons, know it is custom to my environment. I am hope the logic makes sense at least. I am really happy with the way mine works, it took we a few days to nail it down, but well worth it.First, the QueryTask_ExecuteCompleted that has the parcels that fall within the buffer the users creates in a previous step. Also, in here is where I separate the initial selection that the buffer was generated from (blue parcels), and the ones added to the selection by the buffer (red parcels). _bufferitems is a custom class I made just for this task. It made it much easier for me. private void QueryTask_ExecuteCompleted(object sender, QueryEventArgs args)
{
//Reset Selected (blue) graphics
identifySelect.ClearGraphics();
//Add all blue (Selected) graphics to graphics layer
foreach (DataItem x in _bufferSelectedItems)
{
ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
{
Geometry = x.geodata,
Symbol = GlobalVariables.theMainPage.LayoutRoot.Resources["SelectedFillSymbol"] as FillSymbol
};
identifySelect.Graphics.Add(graphic);
}
//Reset Results (red) graphics
identifyResults.ClearGraphics();
// "_bufferList" will hold all if the results from the parcel query and display them in the child window when called
_bufferList = new List<_bufferItems>();
//Add all red (Results) graphics to graphics layer
//And add each result to the buffer csv list
foreach (Graphic feature in args.FeatureSet)
{
string StreetNum = "";
string StreetDir = "";
string Street = "";
string StreetType = "";
feature.Symbol = GlobalVariables.theMainPage.LayoutRoot.Resources["ResultFillSymbol"] as FillSymbol;
bool uniqueGraphic = true;
//If graphic exists in Selection Graphics Layer (_bufferSelectedItems), do not add to Results Graphics Layer
//This allows the user to see the initial buffer selection versus the parcels returned from the buffer parcel query
foreach (DataItem x in _bufferSelectedItems)
{
if (x.Data[StringID.APN].ToString() == feature.Attributes[StringID.APN].ToString())
{
uniqueGraphic = false;
}
}
if (uniqueGraphic)
{
identifyResults.Graphics.Add(feature);
}
//Create a new buffer item
_bufferItems rptItemsSitus = new _bufferItems();
_bufferItems rptItemsOwner = new _bufferItems();
//Gather individual fields to create full address
//This "if" statement returns true if the user is looking for situs or both addresses
if (GlobalVariables.theBufferChoice.rdoBoth.IsChecked == true || GlobalVariables.theBufferChoice.rdoSitus.IsChecked == true)
{
if (feature.Attributes[StringID.csvStNum] != null)
{
StreetNum = feature.Attributes[StringID.csvStNum].ToString().Trim();
}
if (feature.Attributes[StringID.csvStDir] != null)
{
StreetDir = feature.Attributes[StringID.csvStDir].ToString().Trim();
}
if (feature.Attributes[StringID.csvSt] != null)
{
Street = feature.Attributes[StringID.csvSt].ToString().Trim();
}
if (feature.Attributes[StringID.csvStType] != null)
{
StreetType = feature.Attributes[StringID.csvStType].ToString().Trim();
}
//Use "Occupant" for external users
rptItemsSitus.Name = "Occupant";
//Concatinate address
rptItemsSitus.Address = StreetNum + " " + StreetDir + " " + Street + " " + StreetType;
if (feature.Attributes[StringID.csvCity] != null)
{
rptItemsSitus.City = feature.Attributes[StringID.csvCity].ToString();
}
if (feature.Attributes[StringID.csvState] != null)
{
rptItemsSitus.State = feature.Attributes[StringID.csvState].ToString();
}
if (feature.Attributes[StringID.csvZip] != null)
{
rptItemsSitus.Zip = feature.Attributes[StringID.csvZip].ToString();
}
_bufferList.Add(rptItemsSitus);
}
//This "if" statement returns true if the user is looking for owner or both addresses
if (GlobalVariables.theBufferChoice.rdoBoth.IsChecked == true || GlobalVariables.theBufferChoice.rdoOwner.IsChecked == true)
{
if (feature.Attributes[StringID.csvMailAddress] != null)
{
Street = feature.Attributes[StringID.csvMailAddress].ToString().Trim();
}
if (feature.Attributes[StringID.csvFirstLast] != null)
{
rptItemsOwner.Name = feature.Attributes[StringID.csvFirstLast].ToString().Trim();
}
//Concatinate address
rptItemsOwner.Address = Street;
if (feature.Attributes[StringID.csvMailCity] != null)
{
rptItemsOwner.City = feature.Attributes[StringID.csvMailCity].ToString();
}
if (feature.Attributes[StringID.csvMailState] != null)
{
rptItemsOwner.State = feature.Attributes[StringID.csvMailState].ToString();
}
if (feature.Attributes[StringID.csvMailZip] != null)
{
rptItemsOwner.Zip = feature.Attributes[StringID.csvMailZip].ToString();
}
_bufferList.Add(rptItemsOwner);
}
}
//Fill datagrid with results
dgReport.ItemsSource = _bufferList;
//lets the BufferChoice control know that the report has run
GlobalVariables.theSideMenu.buffercontrol.makeChoiceOpenFalse();
}
Then, take the _bufferlist created in the querytask and generate it to a .csvprivate void SaveButton_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog()
{
DefaultExt = "csv",
Filter = "Comma Separated Values (*.csv)|",
FilterIndex = 1
};
int bufferCount = _bufferList.Count;
sfd.ShowDialog();
System.IO.Stream stream = sfd.OpenFile();
StreamWriter sw = new StreamWriter(stream);
sw.Write("\"Name\",\"Address\",\"City\",\"State\",\"Zip\"\r\n");
for (int i = 0; i <= bufferCount - 1; i++)
{
sw.Write("\"" + _bufferList.Name + "\",\"" + _bufferList.Address + "\",\""
+ _bufferList.City + "\",\"" + _bufferList.State + "\",\"" + _bufferList.Zip + "\" \r\n");
}
sw.Close();
stream.Close();
}