void DoWork()
        {
            var filterTerm = textBox1.Text;
            IEnumerable <Entity> privileges = rManager.Privileges;

            if (filterTerm.Length != 0)
            {
                privileges =
                    rManager.Privileges.Where(
                        x => x["name"].ToString().ToLower().IndexOf(filterTerm.ToLower(), StringComparison.Ordinal) >= 0);
            }

            var items = new List <ListViewItem>();

            foreach (var privilege in privileges)
            {
                var groupName = string.Empty;
                var group     = (from entity in entities
                                 where entity.Privileges.Any(p => p.PrivilegeId == privilege.Id)
                                 select new{ entity.LogicalName, entity.DisplayName.UserLocalizedLabel.Label }).FirstOrDefault();
                var entitySchemaName = (from entity in entities
                                        where entity.Privileges.Any(p => p.Name == privilege["name"].ToString())
                                        select entity.SchemaName).FirstOrDefault();

                if (group == null)
                {
                    if (privilege["name"].ToString().EndsWith("Entity"))
                    {
                        groupName = "Entity";
                    }
                    else if (privilege["name"].ToString().EndsWith("Attribute"))
                    {
                        groupName = "Attribute";
                    }
                    else if (privilege["name"].ToString().EndsWith("Relationship"))
                    {
                        groupName = "Relationship";
                    }
                    else if (privilege["name"].ToString().EndsWith("OptionSet"))
                    {
                        groupName = "OptionSet";
                    }
                    else
                    {
                        groupName = "_Common";
                    }
                }
                else
                {
                    if (group.LogicalName == "customeraddress")
                    {
                        groupName =
                            entities.First(x => x.LogicalName == "account").DisplayName.UserLocalizedLabel.Label;
                    }

                    else if (group.LogicalName == "email" || group.LogicalName == "task" ||
                             group.LogicalName == "letter" || group.LogicalName == "phonecall" ||
                             group.LogicalName == "appointment" || group.LogicalName == "serviceappointment" ||
                             group.LogicalName == "campaignresponse" || group.LogicalName == "fax")
                    {
                        groupName =
                            entities.First(x => x.LogicalName == "activitypointer").DisplayName.UserLocalizedLabel.Label;
                    }

                    else
                    {
                        groupName = group.Label;
                    }
                }

                if (ListViewDelegates.GetGroup(lvPrivileges, groupName) == null)
                {
                    ListViewDelegates.AddGroup(lvPrivileges, groupName);
                }

                var item = new ListViewItem
                {
                    Text  = privilege["name"].ToString().Remove(0, 3),
                    Tag   = privilege,
                    Group =
                        groupName != null
                            ? ListViewDelegates.GetGroup(lvPrivileges, groupName)
                            : ListViewDelegates.GetGroup(lvPrivileges, "_Common")
                };

                if (entitySchemaName != null)
                {
                    item.Text = item.Text.Replace(entitySchemaName, "");
                }

                items.Add(item);
            }

            ListViewDelegates.AddRange(lvPrivileges, items);
            ListViewDelegates.SortGroup(lvPrivileges, true);
            ListViewDelegates.Sort(lvPrivileges, true);
        }
        private void DoWork()
        {
            var filterTerm = textBox1.Text;
            IEnumerable <Entity> privileges = rManager.Privileges;

            if (filterTerm.Length != 0)
            {
                privileges =
                    rManager.Privileges.Where(
                        x => x["name"].ToString().ToLower().IndexOf(filterTerm.ToLower(), StringComparison.Ordinal) >= 0);
            }

            var items = new List <ListViewItem>();

            foreach (var privilege in privileges)
            {
                string entitySchemaName      = null;
                var    groupName             = string.Empty;
                var    entitiesWithPrivilege = (from emd in entities
                                                where emd.Privileges.Any(p => p.PrivilegeId == privilege.Id)
                                                select emd).ToList();
                EntityMetadata entity;
                if (entitiesWithPrivilege.Count > 0 && entitiesWithPrivilege.Any(g => g.IsActivity.Value))
                {
                    entity           = entitiesWithPrivilege.FirstOrDefault(g => g.LogicalName == "activitypointer");
                    entitySchemaName = "Activity";
                }
                else
                {
                    entity = entitiesWithPrivilege.FirstOrDefault();
                    if (entity != null)
                    {
                        entitySchemaName = entity.SchemaName;
                    }
                }

                if (entity == null)
                {
                    if (privilege["name"].ToString().EndsWith("Entity"))
                    {
                        groupName = "Entity";
                    }
                    else if (privilege["name"].ToString().EndsWith("Attribute"))
                    {
                        groupName = "Attribute";
                    }
                    else if (privilege["name"].ToString().EndsWith("Relationship"))
                    {
                        groupName = "Relationship";
                    }
                    else if (privilege["name"].ToString().EndsWith("OptionSet"))
                    {
                        groupName = "OptionSet";
                    }
                    else
                    {
                        groupName = "_Common";
                    }
                }
                else
                {
                    if (entity.LogicalName == "customeraddress")
                    {
                        groupName =
                            entities.First(x => x.LogicalName == "account").DisplayName.UserLocalizedLabel.Label;
                    }
                    else if (entity.IsActivity.Value || entity.LogicalName == "bulkoperation")
                    {
                        groupName =
                            entities.First(x => x.LogicalName == "activitypointer").DisplayName.UserLocalizedLabel.Label;
                    }
                    else
                    {
                        groupName = entity.DisplayName.UserLocalizedLabel.Label;
                    }
                }

                if (ListViewDelegates.GetGroup(lvPrivileges, groupName) == null)
                {
                    ListViewDelegates.AddGroup(lvPrivileges, groupName);
                }

                var item = new ListViewItem
                {
                    Text  = privilege["name"].ToString().Remove(0, 3),
                    Tag   = privilege,
                    Group =
                        groupName != null
                            ? ListViewDelegates.GetGroup(lvPrivileges, groupName)
                            : ListViewDelegates.GetGroup(lvPrivileges, "_Common")
                };

                if (entitySchemaName != null)
                {
                    item.Text = item.Text.Replace(entitySchemaName, "");
                }


                items.Add(item);
            }

            ListViewDelegates.AddRange(lvPrivileges, items);
            ListViewDelegates.SortGroup(lvPrivileges, true);
            ListViewDelegates.Sort(lvPrivileges, true);
        }