c# - List.Except method returns the wrong results -


im trying compare 2 lists of type results, , returns entire list of results, doesnt seem filter out.

this code :

  list<results> veranderingen = resultaten2.except(resultaten).tolist();          foreach(results x in veranderingen)         {             messagebox.show("nieuwe data gevonden: " + x.titel + "van de website" + x.url + "");         } 

the code lists gets filled ( less important ) :

 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 zoekcriteria 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 resultaat 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 zoekcriteria 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 resultaat 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 * resultaat zoekcriteriaid = " + refreshid + ";";         command2.executenonquery();          oledbcommand command3 = new oledbcommand();         command3.connection = connection;         command3.commandtext = "delete * zoekcriteria zoekcriteriaid = " + refreshid + ";";         command3.executenonquery();          search.zoekterm = cbzoektermselecteren.text;         search.insertzoekcriteria();         search.searchding();      } 

i think i'm doing wrong in syntax of except method me out?

there 2 ways compare (for equality) 2 results objects (and reference type objects in general):

  • the first way compare values of properties of 2 results objects.

  • the second way compare references themselves. , mean 2 results objects equal if single object have 2 references it. example, can create single results object , put in 2 lists.

the objects in 2 lists different objects, guessing want use first way of comparison.

by default, equality check of reference type objects in c# of second type. however, can override behavior if want.

one way of overriding behavior override equals , gethashcode methods in class. here example:

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

this way, telling system how should test equality objects of type.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -