private async Task UpdateLinksForSite(string url, IEnumerable <WebSiteInfo> links) { var currentDomain = ExtractDomain(url); var currentSite = await _context.WebSites.Include(site => site.LinkedSites).FirstOrDefaultAsync(w => w.Url == url); if (currentSite == null) { var existingLinks = _context.WebSites.ToList().Except(links, new SiteComparer()); var siteExistingLinks = existingLinks.Intersect(links); var siteNotExistingLnks = links.Except(siteExistingLinks); var newLinks = siteExistingLinks.Union(siteNotExistingLnks); var isInteresting = IsInteresting(newLinks); currentSite = new WebSiteInfo { Url = url, Domain = currentDomain, LinkedSites = newLinks.ToList(), IsInteresting = isInteresting }; _context.Add(currentSite); await _context.SaveChangesAsync(); } else if (currentSite.LinkedSites == null || currentSite.LinkedSites.Count == 0) { var existingLinks = _context.WebSites.ToList().Except(links, new SiteComparer()); var siteExistingLinks = existingLinks.Intersect(links); var siteNotExistingLnks = links.Except(siteExistingLinks); var newLinks = siteExistingLinks.Union(siteNotExistingLnks).ToList(); var isInteresting = IsInteresting(newLinks); currentSite.LinkedSites = newLinks; currentSite.IsInteresting = isInteresting; await _context.SaveChangesAsync(); } }