public IEnumerable <DuplicatedDataModel> GetDuplicationGroups(IOrganizationService service)
        {
            var results = CrmHelper.GetAllData(service, Config.TargetEntity, _selectAttributes, Config.MatchAttributes, Config.ShouldIgnoreBlank);
            Func <Entity, IEnumerable <string>, string> groupingFunction = BuildMatchingKey;

            _duplicationGroups = results.ToLookup(e => groupingFunction(e, Config.MatchAttributes));
            //.SelectMany(g => g, (parent, child) => EntityToDynamic(child, parent.Key));

            return(_duplicationGroups.Where(g => g.Count() > 1)
                   .Select(g => new DuplicatedDataModel
            {
                MatchingKey = g.Key,
                DuplicatedData = EntityToDynamic(g.FirstOrDefault(), g.Key) as IDictionary <string, object>,
                DuplicatesCount = g.Count()
            }));
        }
Beispiel #2
0
        private void LoadData(string entity)
        {
            var matchAttributes = dgvFields.Rows.Cast <DataGridViewRow>()
                                  .Where(r => (bool)(r.Cells["colMatch"] as DataGridViewCheckBoxCell).Value)
                                  .Select(r => r.Cells["colLogicalName"].Value.ToString());

            var viewAttritues = dgvFields.Rows.Cast <DataGridViewRow>()
                                .Where(r => (bool)(r.Cells["colView"] as DataGridViewCheckBoxCell).Value)
                                .Select(r => r.Cells["colLogicalName"].Value.ToString());

            //TODO: check at least one column is cheked to match, and view
            if (matchAttributes.Count() == 0 || viewAttritues.Count() == 0)
            {
                MessageBox.Show("Please select at least one field to match and view", "Select Fields", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // add duplicated view columns
            AddViewColumns(dgvDuplicated, matchAttributes);
            dgvDuplicated.Columns.Add("count", "Duplicates Count");
            dgvDuplicated.Columns.Add("key", "Key");
            dgvDuplicated.Columns["key"].Visible = false;

            // add duplicates view columns
            AddViewColumns(dgvDuplicates, viewAttritues);
            dgvDuplicates.Columns.Add(new DataGridViewLinkColumn()
            {
                Name       = "url",
                HeaderText = "Record URL",
                Text       = "Link",
                UseColumnTextForLinkValue = true
            });
            dgvDuplicates.Columns.Add("hiddenurl", "hiddenurl");
            dgvDuplicates.Columns["hiddenurl"].Visible = false;

            // get selected attributes
            var attributes = new List <string>(matchAttributes.Count() + viewAttritues.Count());

            attributes.AddRange(matchAttributes);
            attributes.AddRange(viewAttritues);
            attributes = attributes.Distinct().ToList();

            WorkAsync(new WorkAsyncInfo
            {
                Message = string.Format($"Loading data..."),
                Work    = (worker, args) =>
                {
                    args.Result = CrmHelper.GetAllData(Service, entity, attributes, matchAttributes, chkIgnoreBlank.Checked);
                },
                PostWorkCallBack = (args) =>
                {
                    if (args.Error != null)
                    {
                        MessageBox.Show(args.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    var result = args.Result as IEnumerable <Entity>;
                    if (result != null)
                    {
                        EntityMetadata metadata = null;
                        if (_entities.TryGetValue(entity, out metadata))
                        {
                            FindDuplicated(result, matchAttributes);
                        }
                    }
                }
            });
        }