List.except 方法返回错误的结果

本文关键字:结果 错误 返回 except 方法 List | 更新日期: 2023-09-27 18:30:37

我试图比较 2 个 Results 类型的列表,它不断地只返回整个结果列表,它似乎没有过滤掉任何东西。

这是代码:

  List<Results> Veranderingen = resultaten2.Except(resultaten).ToList();
        foreach(Results x in Veranderingen)
        {
            MessageBox.Show("Nieuwe Data gevonden: " + x.titel + "Van de website" + x.url + "");
        }

填充列表的代码是这样的(不太重要):

 private void Lijst2invullen()
    {
        OleDbConnection connection = new OleDbConnection();
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'Users'martijn'Dropbox'Proftaak Periode 2 Identity'Database11.accdb;
        Persist Security Info=False;";
        connection.Open();
        OleDbCommand cmd2 = new OleDbCommand();
        cmd2.Connection = connection;
        cmd2.CommandText = "SELECT ZoekcriteriaID from Zoekcriteria WHERE ZoekCriteria = '" + Convert.ToString(cbzoektermselecteren.Text) + "';";
        OleDbDataReader reader2 = cmd2.ExecuteReader();
        if (reader2.Read())
        {
            refreshid2 = Convert.ToInt32(reader2["ZoekcriteriaID"]);
        }


        OleDbCommand command5 = new OleDbCommand();
        command5.Connection = connection;
        command5.CommandText = "SELECT Titel, Webadress from Resultaat WHERE ZoekcriteriaID = " + refreshid2 + ";";
        OleDbDataReader reader3 = command5.ExecuteReader();
        while (reader3.Read())
        {
            Results result = new Results();
            result.url = Convert.ToString(reader3["Webadress"]);
            result.titel = Convert.ToString(reader3["Titel"]);
            resultaten2.Add(result);
        }
        reader3.Close();
        label1.Text = "Ziet er goed uit!";
    }
    private void Lijst1invullen()
    {
        OleDbConnection connection = new OleDbConnection();
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'Users'martijn'Dropbox'Proftaak Periode 2 Identity'Database11.accdb;
        Persist Security Info=False;";
        connection.Open();

        OleDbCommand cmd1 = new OleDbCommand();
        cmd1.Connection = connection;
        cmd1.CommandText = "SELECT ZoekcriteriaID from Zoekcriteria WHERE ZoekCriteria = '" + Convert.ToString(cbzoektermselecteren.Text) + "';";
        OleDbDataReader reader1 = cmd1.ExecuteReader();
        if (reader1.Read())
        {
            refreshid = Convert.ToInt32(reader1["ZoekcriteriaID"]);
        }

        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = "SELECT Titel, Webadress from Resultaat WHERE ZoekcriteriaID = " + refreshid + ";";
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Results result = new Results();
            result.url = Convert.ToString(reader["Webadress"]);
            result.titel = Convert.ToString(reader["Titel"]);
            resultaten.Add(result);
        }
        reader.Close();
        reader1.Close();
        OleDbCommand command2 = new OleDbCommand();
        command2.Connection = connection;
        command2.CommandText = "DELETE * FROM Resultaat WHERE ZoekcriteriaID = " + refreshid + ";";
        command2.ExecuteNonQuery();
        OleDbCommand command3 = new OleDbCommand();
        command3.Connection = connection;
        command3.CommandText = "DELETE * FROM Zoekcriteria WHERE ZoekCriteriaID = " + refreshid + ";";
        command3.ExecuteNonQuery();
        search.zoekterm = cbzoektermselecteren.Text;
        search.InsertZoekcriteria();
        search.searchding();
    }

我想我在 Except 方法的语法中做错了什么,有人可以帮助我吗?

List.except 方法返回错误的结果

有两种

方法可以比较(为了相等)两个Results对象(以及一般的所有引用类型对象):

  • 第一种方法是比较两个Results对象的属性值。

  • 第二种方法是比较参考文献本身。我的意思是,如果两个Results对象实际上是单个对象,但你有两个对它的引用,那么它们就是相等的。例如,您可以创建一个Results对象并将其放在两个列表中。

两个列表中的对象显然是不同的对象,所以我猜你想使用第一种比较方式。

默认情况下,C# 中引用类型对象的相等性检查属于第二种类型。但是,如果需要,可以覆盖此行为。

重写此行为的一种方法是重写类中的 EqualsGetHashCode 方法。下面是一个示例:

public class Results
{
    public string url { get; set; }
    public string title { get; set; }
    public override bool Equals(object obj)
    {
        Results other = obj as Results;
        if (other == null)
            return false;
        return other.url == this.url && other.title == this.title;
    }
    public override int GetHashCode()
    {
        return new {url, title}.GetHashCode();
    }
}

这样,我们告诉系统应该如何测试此类对象的相等性。

我相信 Yacoub 的答案会起作用,并且在大多数情况下可能是首选解决方案,但如果您无法修改 Results 类,这里是另一种方法。定义 IEqualityComparer for Results 的实现,然后将其作为 except 方法的第二个参数传递。编辑:

class Results {
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
class ResultsEqualityComparer : IEqualityComparer<Results> {
    public bool Equals(Results res1, Results res2) {
        return (res1.FirstName == res2.FirstName && res1.LastName == res2.LastName);
    }
    public int GetHashCode(Results res) {
        return new { res.FirstName, res.LastName }.GetHashCode();
    }
}

以下是将 ResultsComparer 传递给 ResultsComparer 的代码示例,但

        var resultaten = new List<Results>() {
        new Results() {FirstName="Bob", LastName="Smith"},
        new Results() {FirstName="Ted", LastName="Wilson"},
        new Results() {FirstName="Alice", LastName="Wilson"},
        new Results() {FirstName="Carol", LastName="Smith"}
        };
        var resultaten2 = new List<Results>() {
        new Results() {FirstName="William", LastName="Smith"},
        new Results() {FirstName="Ted", LastName="Wilson"},
        new Results() {FirstName="Gerhardt", LastName="Wilson"},
        new Results() {FirstName="Carol", LastName="Smith"}
        };
        var comparer = new ResultsEqualityComparer();
        List<Results> Veranderingen = resultaten2.Except(resultaten, comparer).ToList();
        foreach (var x in Veranderingen) {
            Console.WriteLine(x.FirstName + " " + x.LastName);
        }

打印输出:威廉·史密斯格哈特·威尔逊