c# - Why am I not getting results when performing an intersection? -


users class:

public class user   {     public int id { get; set; }     public string email { get; set; } } 

code:

        var usersl = new list<user>()                         {                             new user{id = 1,email = "abc@foo.com"},                             new user{id = 2,email = "def@foo.com"}                         };          var usersr = new list<user>()                         {                             new user{id = 1,email = "abc@foo.com"},                             new user{id = 2,email = "def@foo.com"}                         };          var both = (from l in usersl select l)             .intersect(from users in usersr select users);          foreach (var r in both)             console.writeline(r.email); 

which returns 0 results.

i know can accomplish similar using join, want use intersect because a) going work on db code , want use function (too long go why) , b) i'm plain curious why intersect isn't working here.

        var both = l in usersl                    join r in usersr on l.id equals r.id                    select l; 

.net provides comparison logic predefined types. in case of join query joining (comparing) 2 ids of type int (predefined types)

var both = l in usersl            join r in usersr on l.id equals r.id            select l; 

in case of intersect query trying compare 2 user defined custom objects of type user. hence need provide own custom compare implementation logic.

there 2 ways tackle this...

option 1: implement iequalitycomparer

public class user {     public int id { get; set; }     public string email { get; set; } }  public class myequalitycomparer : iequalitycomparer<user> {     public bool equals(user x, user y)     {         if (object.referenceequals(x, y))             return true;         if (x == null || y == null)             return false;         return x.id.equals(y.id) &&                x.email.equals(y.email);     }      public int gethashcode(user u)     {         return new { u.id, u.email }.gethashcode();     } }  class program {     static void main(string[] args)     {          var usersl = new list<user>()                     {                         new user{id = 1,email = "abc@foo.com"},                         new user{id = 2,email = "def@foo.com"}                     };          var usersr = new list<user>()                     {                         new user{id = 1,email = "abc@foo.com"},                         new user{id = 2,email = "def@foo.com"}                     };          var both =  (from l in usersl select l)           .intersect(from users in usersr select users, new myequalitycomparer());          foreach (var r in both)             console.writeline(r.email);     } } 

option 2: override equals , gethashcode methods in custom object itself

public class user  {     public int id { get; set; }     public string email { get; set; }      public override bool equals(object obj)     {         // check null values , compare run-time types.         if (obj == null || gettype() != obj.gettype())             return false;          user x = (user)obj;         return (id == x.id) && (email == x.email);     }      public override int gethashcode()     {         return new { id, email }.gethashcode();     } }  class program {     static void main(string[] args)     {          var usersl = new list<user>()             {                 new user{id = 1,email = "abc@foo.com"},                 new user{id = 2,email = "def@foo.com"}             };          var usersr = new list<user>()             {                 new user{id = 1,email = "abc@foo.com"},                 new user{id = 2,email = "def@foo.com"}             };          var both = (from l in usersl select l)           .intersect(from users in usersr select users);          foreach (var r in both)             console.writeline(r.email);     } } 

hope helps.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -