开发者

Check for child duplicates

开发者 https://www.devze.com 2023-02-13 09:29 出处:网络
My console app will loop through each User to get their Websites, so that it can take new screenshots of them. However, to prevent taking screenshot of the same website twice I have to check whether t

My console app will loop through each User to get their Websites, so that it can take new screenshots of them. However, to prevent taking screenshot of the same website twice I have to check whether there already has been taken screenshot of the website, while looping through another users websites.

My current solution is:

Database:

User
|--> ID: 1
|--> FirstName: Joe

|--> ID: 2
|--> FirstName: Stranger

Websites
|--> ID: 1
|--> UserID: 1
|--> URL: http://site.com

|--> ID: 2
|--> UserID: 2
|--> URL: http://site.com

Console app:

static void RenewWebsiteThumbNails()
{
    Console.WriteLine("Starting renewal process...");

    using (_repository)
    {
        var websitesUpdated = new List<string>();

        foreach (var user in _repository.GetAll())
        {
            foreach (var website in user.Websit开发者_如何学Goes.Where(website => !websitesUpdated.Contains(website.URL)))
            {
                _repository.TakeScreenDumpAndSave(website.URL);
                websitesUpdated.Add(website.URL);

                Console.WriteLine(new string('-', 50));
                Console.WriteLine("{0} has successfully been renewed", website.URL);
            }
        }
    }
}

However, it seems wrong to declare a List for such a scenario, just to check whether a specific URL already has been added... any suggestions for an alternative way?


You can use

 var websitesUpdated = new HashSet<string>();

Cost of the operation O(1) instead of O(n) in list case.

EDIT: By the way I would take all urls from each user and put them all in one HashSet so there won't be any duplicates and then just iterate on HashSet as it is a simple list.

Some think like this.

var websites = new HashSet<string>();
foreach (var url in   _repository.GetAll().SelectMany(user=>user.Websites))
  websites.Add(url);

After this,

foreach (var website in websites)
{
Console.WriteLine(new string('-', 50)); 
Console.WriteLine("{0} has successfully been renewed",website.URL);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消