Comparing 2 lists
-
I want to compare 2 lists i should get result if any of their property matches I have this code
var list1 = new List<Emp>();
var list2 = new List<Emp>();list1.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "Sean", ID = 3 }); foreach(var p in list1.Count()>list2.Count() ? list2:list1 ) { var u=list2.Where(x => x.ID == p.ID || x.Empname == p.Empname ); }
This works fine but results are displayed twice it should be only one. currently i get {nitin,1} twice in u. where is the issue.
-
I want to compare 2 lists i should get result if any of their property matches I have this code
var list1 = new List<Emp>();
var list2 = new List<Emp>();list1.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "Sean", ID = 3 }); foreach(var p in list1.Count()>list2.Count() ? list2:list1 ) { var u=list2.Where(x => x.ID == p.ID || x.Empname == p.Empname ); }
This works fine but results are displayed twice it should be only one. currently i get {nitin,1} twice in u. where is the issue.
Don't use Linq, and
break
after the first match. Or maybe use distinct? Is there one? -
I want to compare 2 lists i should get result if any of their property matches I have this code
var list1 = new List<Emp>();
var list2 = new List<Emp>();list1.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "Sean", ID = 3 }); foreach(var p in list1.Count()>list2.Count() ? list2:list1 ) { var u=list2.Where(x => x.ID == p.ID || x.Empname == p.Empname ); }
This works fine but results are displayed twice it should be only one. currently i get {nitin,1} twice in u. where is the issue.
That's what the selection on list2 will give. (Think about it for a moment.) What you want is something along the lines of
var matches = list1.Where(x => list2.Contains(x));
Since you apparently want a value comparison you should override Equals (and maybe == too) on Emp. That would test that all the properties matched (and I think it would make Contains work in the above expression). If you really want to check that any of the properties match, you need to write a Contains equivalent (it's a one line LINQ expression). But that seems unlikely. Ideally you'd be using the same actual object for what appears to be a reference to the same database row, but if it's actually coming out of a database provider that may not be possible. Classes that represent database entities often want value semantics for equality (unless you're using some clever entity management framework that gives you the same instance for the same row).
-
I want to compare 2 lists i should get result if any of their property matches I have this code
var list1 = new List<Emp>();
var list2 = new List<Emp>();list1.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "Sean", ID = 3 }); foreach(var p in list1.Count()>list2.Count() ? list2:list1 ) { var u=list2.Where(x => x.ID == p.ID || x.Empname == p.Empname ); }
This works fine but results are displayed twice it should be only one. currently i get {nitin,1} twice in u. where is the issue.
Just use a First query on the end of your Where query like this
foreach(var p in list1.Count()>list2.Count() ? list2:list1 )
{
var u=list2.Where(x => x.ID == p.ID || x.Empname == p.Empname ).FirstOrDefault();
}and that will return the first such emp. I use FirstOrDefault() in case there are no matches it will return a null Emp instead of throwing an Exception if there are no matches. You need to look out if List2 is longer than List1 as you will then be matching List2 to List2 here
list2.Where
. Hope this helpsWhen I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
I want to compare 2 lists i should get result if any of their property matches I have this code
var list1 = new List<Emp>();
var list2 = new List<Emp>();list1.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "nitin", ID = 1 }); list2.Add(new Emp { Empname = "Sean", ID = 3 }); foreach(var p in list1.Count()>list2.Count() ? list2:list1 ) { var u=list2.Where(x => x.ID == p.ID || x.Empname == p.Empname ); }
This works fine but results are displayed twice it should be only one. currently i get {nitin,1} twice in u. where is the issue.
Since you are adding Emp{Empname="nitin", ID=1} twice I guess finding it twice in your foreach shouldn't be that much of a surprise. I would use intersect. That means implementing IEquatable for your class (and perhaps GetHashCode). Like this:
class Emp : IEquatable<Emp>
{
public string Empname { get; set; }
public int ID { get; set; }#region IEquatable<Emp> Members
public bool Equals(Emp other)
{
if (Object.ReferenceEquals(other, null)) return false;
if (Object.ReferenceEquals(other, this)) return true;return ID == other.ID && Empname.Equals(other.Empname);
}
#endregion
public override int GetHashCode()
{
//Calculate hash
return Empname == null ? 0 : Empname.GetHashCode()^
ID.GetHashCode();
}
}And then use it as:
var u = list2.Intersect(list1);
-
Since you are adding Emp{Empname="nitin", ID=1} twice I guess finding it twice in your foreach shouldn't be that much of a surprise. I would use intersect. That means implementing IEquatable for your class (and perhaps GetHashCode). Like this:
class Emp : IEquatable<Emp>
{
public string Empname { get; set; }
public int ID { get; set; }#region IEquatable<Emp> Members
public bool Equals(Emp other)
{
if (Object.ReferenceEquals(other, null)) return false;
if (Object.ReferenceEquals(other, this)) return true;return ID == other.ID && Empname.Equals(other.Empname);
}
#endregion
public override int GetHashCode()
{
//Calculate hash
return Empname == null ? 0 : Empname.GetHashCode()^
ID.GetHashCode();
}
}And then use it as:
var u = list2.Intersect(list1);