我试图比较结果类型的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方法的语法中做错了什么可以帮助我?
答案 0 :(得分:4)
有两种比较(相等)两个Results
对象(以及所有引用类型对象)的方法:
第一种方法是比较两个Results
对象的属性值。
第二种方法是比较参考文献本身。我的意思是,如果两个Results
对象实际上是一个单独的对象,那么它们是相等的,但是你有两个对象。例如,您可以创建一个Results
对象并将其放在两个列表中。
两个列表中的对象显然是不同的对象,所以我猜你想要使用第一种比较方式。
默认情况下,C#中引用类型对象的相等性检查属于第二种类型。但是,如果需要,可以覆盖此行为。
覆盖此行为的一种方法是覆盖类中的Equals
和GetHashCode
方法。这是一个例子:
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();
}
}
这样,我们告诉系统应该如何测试此类对象的相等性。
答案 1 :(得分:1)
我确信Yacoub的答案可行,在大多数情况下可能是首选解决方案,但如果您无法修改Results类,这是另一种方法。为Results定义IEqualityComparer的实现,然后将其作为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传递给Except:
的代码示例 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);
}
//打印出: 威廉史密斯 格哈德威尔逊