gpt4 book ai didi

c# - List.Except 方法返回错误结果

转载 作者:行者123 更新时间:2023-12-04 00:43:19 25 4
gpt4 key购买 nike

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

这是代码:

  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 方法的语法中做错了什么,有人可以帮助我吗?

最佳答案

有两种方法可以比较(是否相等)两个 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();
}
}

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

关于c# - List.Except 方法返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34833269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com