C# 如何在两个列表中循环以比较两个列表中的项目?

C# 如何在两个列表中循环以比较两个列表中的项目?,c#,C#,我有以下代码: private void removeDuplicates(List<string> currentSites, List<string> visitedSites) { for (int i = 0; i < currentSites.Count; i++) { for (int x = 0; x < visitedSites.Count; x++

我有以下代码:

private void removeDuplicates(List<string> currentSites, List<string> visitedSites)
        {
            for (int i = 0; i < currentSites.Count; i++)
            {
                for (int x = 0; x < visitedSites.Count; x++)
                {

                }
            }                    
        }
private void removeDuplicates(列出当前站点,列出访问站点)
{
对于(int i=0;i
我得到两个列表,我需要首先将一个列表中的每个项目与另一个列表中的项目进行比较,以循环其他列表中的所有项目并进行比较。如果其中一项存在于另一个列表中,则将其标记为NULL

我需要检查visitedSites是否在currentSites中,以便在所有列表上移动一个项目,检查是否退出,是否将其标记为null

在任何情况下,我都需要使用两个循环,一个是ine,另一个是ine

当我发现它为空时,将它标记为空,并在它断开之后

然后我需要为添加另一个循环,以便在列表currentSites上移动(如果im没有错误),并删除所有标记为NULL的项

其思想是通过将重复项标记为null来比较列表,然后删除所有null项

这是从一开始的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Net;
using System.Web;


namespace GatherLinks
{
    public partial class Form1 : Form
    {
        List<string> currentCrawlingSite;
        List<string> sitesToCrawl;
        int actual_sites;
        BackgroundWorker worker;
        int sites = 0;
        int y = 0;
        string guys = "http://www.google.com";

        public Form1()
        {
            InitializeComponent();

            currentCrawlingSite = new List<string>();
            sitesToCrawl = new List<string>();
            actual_sites = 0;
                    }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private List<string> getLinks(HtmlAgilityPack.HtmlDocument document)
        {

            List<string> mainLinks = new List<string>();
            var linkNodes = document.DocumentNode.SelectNodes("//a[@href]");
            if (linkNodes != null)
            {
                foreach (HtmlNode link in linkNodes)
                {
                    var href = link.Attributes["href"].Value;
                    mainLinks.Add(href);
                }
            }
            return mainLinks;

        }


        private List<string> webCrawler(string url, int levels , DoWorkEventArgs eve)
        {
                HtmlAgilityPack.HtmlDocument doc;
                HtmlWeb hw = new HtmlWeb();
                List<string> webSites;// = new List<string>();
                List<string> csFiles = new List<string>();

                csFiles.Add("temp string to know that something is happening in level = " + levels.ToString());
                csFiles.Add("current site name in this level is : " + url);
                                try
                {
                    doc = hw.Load(url);
                    currentCrawlingSite.Add(url);
                    webSites = getLinks(doc);
                    removeDuplicates(currentCrawlingSite, webSites);
                    removeDuplicates(currentCrawlingSite, sitesToCrawl);
                    sitesToCrawl = webSites;



                    if (levels == 0)
                    {
                        return csFiles;
                    }
                    else
                    {


                        for (int i = 0; i < webSites.Count() && i < 20; i++)                         {
                            int mx = Math.Min(webSites.Count(), 20);

                            if ((worker.CancellationPending == true))
                            {
                                eve.Cancel = true;
                                break;
                            }
                            else
                            {

                                string t = webSites[i];
                                                                if ((t.StartsWith("http://") == true) || (t.StartsWith("https://") == true)) 
                                {

                                        actual_sites++;
                                        csFiles.AddRange(webCrawler(t, levels - 1,eve));
                                        this.Invoke(new MethodInvoker(delegate { Texts(richTextBox1, "Level Number " + levels + " " + t + Environment.NewLine, Color.Red); }));
                                        worker.ReportProgress(Math.Min((int)((double)i / mx * 100),100));



                                }
                            }
                        }

                        return csFiles;
                    }



                }
                catch
                {
                    return csFiles;
                }

        }
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用HtmlAgilityPack;
使用System.IO;
使用System.Text.RegularExpressions;
使用System.Xml.Linq;
Net系统;
使用System.Web;
命名空间聚合链接
{
公共部分类Form1:Form
{
列出当前爬网站点;
列表站点抓取;
int实际_站点;
背景工人;
int站点=0;
int y=0;
串家伙=”http://www.google.com";
公共表格1()
{
初始化组件();
CurrentScrawlingSite=新列表();
sitesToCrawl=新列表();
实际_站点=0;
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有列表获取链接(HtmlAgilityPack.HtmlDocument文档)
{
List mainLinks=新列表();
var linkNodes=document.DocumentNode.SelectNodes(“//a[@href]”);
if(linkNodes!=null)
{
foreach(链接节点中的HtmlNode链接)
{
var href=link.Attributes[“href”].Value;
mainLinks.Add(href);
}
}
返回主链接;
}
私有列表webCrawler(字符串url、整数级别、DoWorkEventArgs eve)
{
HtmlAgilityPack.HtmlDocument文档;
HtmlWeb hw=新的HtmlWeb();
列出网站;//=新建列表();
List csFiles=新列表();
csFiles.Add(“临时字符串,以了解level=“+levels.ToString())中正在发生的事情”;
csFiles.Add(“此级别中的当前站点名称为:“+url”);
尝试
{
文档=硬件加载(url);
CurrentScrawlingSite.Add(url);
网站=获取链接(doc);
移除的副本(当前爬网站点、网站);
移除的副本(当前爬网站点、站点爬网);
sitesToCrawl=网站;
如果(级别==0)
{
返回CSF文件;
}
其他的
{
对于(int i=0;i

因此,我两次调用RemovedUpplicated函数需要在RemovedUpplicated中执行我上面写的操作,然后我不确定是否要执行sitesToCrawl=webSites;或者是否要以某种方式将网站中的链接添加到sitesToCrawl。我的想法是,当我在网站上循环时,在添加到csFiles列表时将不会有重复的项目。

当然,如果我理解你的问题:

IEnumerable<string> notVisitedSites = currentSites.Except(visitedSites);
IEnumerable notVisitedSites=currentSites.Exception(visitedSites);

那么您的问题是什么?您的代码可以编写得更高效、更短,但您这里的代码是否在某种程度上不起作用?如果它不起作用,那么它不起作用呢?