internal object ParseGroupByResponse(DuplicateQueryBuilder schema)
        {
            var sampleDataSet = new DataSet {Locale = CultureInfo.InvariantCulture};
            var output = sampleDataSet.Tables.Add("Duplicates");
            output.Columns.Add(schema.DuplicateColumnName, typeof(int));

            SetTableColumnsFromSchema(schema, output);

            foreach (var response in _response.Entities)
            {
                var countValue = GetAliasedValue(response, schema.DuplicateColumnName);

                if ((int) countValue == 1)
                    continue;

                var row = output.NewRow();
                row[schema.DuplicateColumnName] = countValue;

                foreach (var attribute in schema.Attributes)
                {
                    if (attribute.IsDate)
                    {
                        var groupedDay = GetAliasedValue(response, "GroupByDay");
                        var groupedMonth = GetAliasedValue(response, "GroupByMonth");
                        var groupedYear = GetAliasedValue(response, "GroupByYear");
                        row[attribute.Name] = groupedMonth + "/" + groupedDay + "/" + groupedYear;
                    }
                    else if (attribute.IsLookup)
                    {
                        row[attribute.Name] = GetAliasedValue(response, "GroupBy" + attribute.Name);
                        row[attribute.Name + "name"] = GetAliasedName(response, "GroupBy" + attribute.Name);
                    }
                    else
                    {
                        row[attribute.Name] = GetAliasedValue(response, "GroupBy" + attribute.Name);
                    }
                }
                output.Rows.Add(row);
            }

            output.AcceptChanges();
            return output;
        }
        private void QueryDuplicates()
        {
            WorkAsync(
                string.Format("Querying Duplicates [Entity:{0} - Attributes Selected:{1}", entityDropdown.SelectedValue,
                    entityAttributeView.RowCount),
                e => // Work To Do Asynchronously
                {
                    var fetchXmlBuilder = new DuplicateQueryBuilder();
                    var formEntryReader = new FormEntryReader(fetchXmlBuilder);
                    formEntryReader.ReadEntitySelected(entityDropdown);
                    formEntryReader.ReadUniqueIdentifierAttributes(entityAttributeView);
                    fetchXmlBuilder.AddCustomFilterXml(customXMLFilterBox.Text);
                    e.Result = GetRecordsFromCrm(fetchXmlBuilder.GetOutput());

                },
                e => // Cleanup when work has completed
                {
                    var reader = new FetchXmlResponseReader(e.Result as EntityCollection);

                    var fetchXmlBuilder = new DuplicateQueryBuilder();
                    var formEntryReader = new FormEntryReader(fetchXmlBuilder);
                    formEntryReader.ReadEntitySelected(entityDropdown);
                    formEntryReader.ReadUniqueIdentifierAttributes(entityAttributeView);
                    fetchXmlBuilder.AddCustomFilterXml(customXMLFilterBox.Text);
                    var dataSet = reader.ParseGroupByResponse(fetchXmlBuilder);
                    SafeSetDataSource(duplicatesGrid, dataSet);
                });
        }
 private void Output_Debug(object sender, EventArgs e)
 {
     var fetchXmlBuilder = new DuplicateQueryBuilder();
     var formEntryReader = new FormEntryReader(fetchXmlBuilder);
     formEntryReader.ReadEntitySelected(entityDropdown);
     formEntryReader.ReadUniqueIdentifierAttributes(entityAttributeView);
     fetchXmlBuilder.AddCustomFilterXml(customXMLFilterBox.Text);
     var query = fetchXmlBuilder.GetOutput().Query;
     MessageBox.Show(string.Format("Hit CTRL - C to copy the query below: {0}", query));
 }