public ActionResult NameAuthorities()
        {
            var results    = GetNameAllDocs();
            var agentLinks = AsRepo.GetLinkedAgents();

            var csv = new StringBuilder();

            csv.AppendLine("id,authoritativeLabel,externalAuthorityUri,archivesSpaceUri,type,rid,found,auth_id_exists");

            foreach (var result in results)
            {
                AgentGroup found;
                var        match     = false;
                var        authMatch = false;

                if (!result.doc._id.StartsWith("_"))
                {
                    var externalUri = string.Empty;
                    if (result.doc.externalAuthorityUri != null)
                    {
                        externalUri = result.doc.externalAuthorityUri;
                    }
                    else if (result.doc.substrings != null && result.doc.substrings.Count > 0)
                    {
                        externalUri = string.Join(";", result.doc.substrings?.Select(x => x.externalAuthorityUri));
                    }

                    if (result.doc.creator != null)
                    {
                        foreach (var item in result.doc.creator)
                        {
                            found = agentLinks.Find(x => x.person_name == result.doc.authoritativeLabel && x.resource_id.ToString() == item.Replace("http://archivesspace.ecu.edu/resources/", "") && x.role_id == 878);
                            if (found != null)
                            {
                                match     = true;
                                authMatch = result.doc._id == found.authority_id;
                            }
                            csv.AppendLine($"{result.doc._id},\"{result.doc.authoritativeLabel}\",{externalUri},{result.doc.archivesSpaceUri},creator,{item.Replace("http://archivesspace.ecu.edu/resources/", "")},{match},{authMatch}");
                        }
                    }
                    if (result.doc.source != null)
                    {
                        foreach (var item in result.doc.source)
                        {
                            found = agentLinks.Find(x => x.person_name == result.doc.authoritativeLabel && x.resource_id.ToString() == item.Replace("http://archivesspace.ecu.edu/resources/", "") && x.role_id == 879);
                            if (found != null)
                            {
                                match     = true;
                                authMatch = result.doc._id == found.authority_id;
                            }
                            csv.AppendLine($"{result.doc._id},\"{result.doc.authoritativeLabel}\",{externalUri},{result.doc.archivesSpaceUri},source,{item.Replace("http://archivesspace.ecu.edu/resources/", "")},{match},{authMatch}");
                        }
                    }

                    if (result.doc.creator == null && result.doc.source == null)
                    {
                        csv.AppendLine($"{result.doc._id},\"{result.doc.authoritativeLabel}\",,{result.doc.archivesSpaceUri},,FALSE,{match},{authMatch}");
                    }
                }
            }

            System.IO.File.WriteAllText(Server.MapPath("~/Download/nameAuths.csv"), csv.ToString(), Encoding.UTF8);
            return(Redirect("~/Download/nameAuths.csv"));
        }
        public ActionResult AsNameReport()
        {
            var agentLinks = AsRepo.GetLinkedAgents();
            var authority  = GetNameAllDocs();

            var csv = new StringBuilder();

            csv.AppendLine("resource_id,repo,role_id,agent_person_id,person_name,agent_family_id,family_name,agent_corporate_entity_id,corp_name,found,auth_id_exists");

            foreach (var link in agentLinks)
            {
                Row found     = null;
                var match     = false;
                var authMatch = false;
                var repo      = link.repo_id == 2 ? "Archives" : link.repo_id == 3 ? "Manuscripts" : "Laupus";
                if (link.agent_corporate_entity_id != null)
                {
                    if (link.role_id == 878)
                    {
                        found = authority.Find(x => x.doc.authoritativeLabel == link.corp_name && x.doc.corporateNameCreator != null && x.doc.corporateNameCreator.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && x.doc.archivesSpaceUri == "http://archivesspace.ecu.edu/agents/agent_corporate_entity/" + link.agent_corporate_entity_id);
                    }
                    else if (link.role_id == 879)
                    {
                        found = authority.Find(x => x.doc.authoritativeLabel == link.corp_name && x.doc.corporateNameSource != null && x.doc.corporateNameSource.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && x.doc.archivesSpaceUri == "http://archivesspace.ecu.edu/agents/agent_corporate_entity/" + link.agent_corporate_entity_id);
                    }

                    if (found != null)
                    {
                        match     = true;
                        authMatch = found.doc._id == link.authority_id;
                    }
                    csv.AppendLine($"{link.resource_id},{repo},{link.role_id},{link.agent_person_id},\"{link.person_name}\",{link.agent_family_id},\"{link.family_name}\",{link.agent_corporate_entity_id},\"{link.corp_name}\",{match},{authMatch}");
                }
                else if (link.agent_person_id != null)
                {
                    if (link.role_id == 878)
                    {
                        found = authority.Find(x => x.doc.authoritativeLabel == link.person_name && x.doc.personalNameCreator != null && x.doc.personalNameCreator.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && x.doc.archivesSpaceUri == "http://archivesspace.ecu.edu/agents/agent_person/" + link.agent_person_id);
                    }
                    else if (link.role_id == 879)
                    {
                        found = authority.Find(x => x.doc.authoritativeLabel == link.person_name && x.doc.personalNameSource != null && x.doc.personalNameSource.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && x.doc.archivesSpaceUri == "http://archivesspace.ecu.edu/agents/agent_person/" + link.agent_person_id);
                    }

                    if (found != null)
                    {
                        match     = true;
                        authMatch = found.doc._id == link.authority_id;
                    }
                    csv.AppendLine($"{link.resource_id},{repo},{link.role_id},{link.agent_person_id},\"{link.person_name}\",{link.agent_family_id},\"{link.family_name}\",{link.agent_corporate_entity_id},\"{link.corp_name}\",{match},{authMatch}");
                }
                else if (link.agent_family_id != null)
                {
                    if (link.role_id == 878)
                    {
                        found = authority.Find(x => x.doc.authoritativeLabel == link.family_name && x.doc.familyNameCreator != null && x.doc.familyNameCreator.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && x.doc.archivesSpaceUri == "http://archivesspace.ecu.edu/agents/agent_family/" + link.agent_family_id);
                    }
                    else if (link.role_id == 879)
                    {
                        found = authority.Find(x => x.doc.authoritativeLabel == link.family_name && x.doc.familyNameSource != null && x.doc.familyNameSource.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && x.doc.archivesSpaceUri == "http://archivesspace.ecu.edu/agents/agent_family/" + link.agent_family_id);
                    }

                    if (found != null)
                    {
                        match     = true;
                        authMatch = found.doc._id == link.authority_id;
                    }
                    csv.AppendLine($"{link.resource_id},{repo},{link.role_id},{link.agent_person_id},\"{link.person_name}\",{link.agent_family_id},\"{link.family_name}\",{link.agent_corporate_entity_id},\"{link.corp_name}\",{match},{authMatch}");
                }
            }

            System.IO.File.WriteAllText(Server.MapPath("~/Download/AsNameReport.csv"), csv.ToString(), Encoding.UTF8);
            return(Redirect("~/Download/AsNameReport.csv"));
        }