public static bool AddRelatedCompanies(this DCDContext dc, int companyId, List <CompanyPath> paths) { try { foreach (CompanyPath path in paths) { RelatedCompany relatedCompany = dc.RelatedCompanies.FirstOrDefault( c => c.Company.RecordId == companyId && c.RelatedCompanyRecordNumber == path.id); if (relatedCompany == null) { relatedCompany = new RelatedCompany(); relatedCompany.CompanyRecordId = companyId; relatedCompany.RelatedCompanyRecordNumber = path.id; relatedCompany.RelatedCompanyPath = path.Value; dc.RelatedCompanies.InsertOnSubmit(relatedCompany); } else { relatedCompany.RelatedCompanyPath = path.Value; } } dc.SubmitChanges(); return(true); } catch (Exception e) { return(false); } }
private IEnumerable <CompanyWrapper> GetWithRelated() { DCDManager dcdMgr = new DCDManager(); // build a dictionary of all of the companies, key is the company id Dictionary <int, Company> allCompanies = dcdMgr.GetAllCompanies().ToDictionary(x => x.RecordId, x => x); // pull down a list of all related companies RelatedCompany[] allRelatedCompanies = dcdMgr.GetAllRelatedCompanies().ToArray(); // our results dictionary, key is the company id Dictionary <int, CompanyWrapper> results = new Dictionary <int, CompanyWrapper>(); // keep track of where a company is when it's inserted into "RelatedCompanies" so we don't have to dig through every companies // RelatedCompanies to try to find a company. Dictionary <int, List <CompanyWrapper> > pointers = new Dictionary <int, List <CompanyWrapper> >(); // gather all of the "Root Companies" because there are a ton of duplicates. RelatedCompany[] rootCompanies = (from i in allRelatedCompanies where i.CompanyRecordId.ToString() == i.RelatedCompanyRecordNumber orderby i.RelatedCompanyPath ascending select i).ToArray(); // build out the wrappers we're going to return. foreach (var relatedCompany in rootCompanies) { Company currentCompany = allCompanies[relatedCompany.CompanyRecordId]; CompanyWrapper wrapper = new CompanyWrapper { RecordID = currentCompany.RecordId, RecordNumber = currentCompany.RecordNumber, Title = currentCompany.Title, Parent = null }; results.Add(relatedCompany.CompanyRecordId, wrapper); } // iterate through all of our root companies foreach (var relatedCompany in rootCompanies) { // split the path by '/' to get all of the seperate results. string[] pieces = relatedCompany.RelatedCompanyPath.Split('/'); // ignore paths with no slash, because they don't have parents. if (pieces.Length == 1) { continue; } // join all of the pieces except for the last piece to give us the parent of the current item string parent = string.Join("/", pieces.Take(pieces.Length - 1).ToArray()); // get the parent from our root companies RelatedCompany parentCompany = rootCompanies.FirstOrDefault(x => x.RelatedCompanyPath == parent); // check to make sure it's legit and isn't itself for some reason. if (parentCompany == null || parentCompany.CompanyRecordId == relatedCompany.CompanyRecordId) { continue; } // get the parent from the "results" dictionary CompanyWrapper parentWrapper = Get(results, pointers, parentCompany.CompanyRecordId, false); // check to make sure it exists. if (parentWrapper == null) { continue; } // get the current item from the results collection and delete it from wherever it is. CompanyWrapper current = Get(results, pointers, relatedCompany.CompanyRecordId, true); if (current == null) { continue; } // keep track of where we're putting it. pointers[current.RecordID] = parentWrapper.RelatedCompanies; if (parentWrapper.RelatedCompanies == null) { parentWrapper.RelatedCompanies = new List <CompanyWrapper>(); } // add it to it's parent. parentWrapper.RelatedCompanies.Add(current); } // just select the values (the CompanyWrappers) List <CompanyWrapper> returnWrappers = results.Select(x => x.Value).ToList(); // recursively sort all of the results Sort(returnWrappers); return(returnWrappers); }
public async Task <int> CreateRelatedCompany(RelatedCompany relatedCompany) { var result = await this.httpService.Post <RelatedCompany>("/relatedCompanies", relatedCompany); return(result.Id); }