Ih, i am facing a problem with IEquatable (C#). As you can see in the following code, I got a class where i've implement IEquatable but it's "Equals" method is not getting reach. My objective is:
I have a datetime column in my database and i would like to distinct only date, not considering the "time" part.
for example: 12-01-2014 23:14 would be equal to 12-01-2014 18:00.
namespace MyNamespace
{
public class MyRepository
{
public void MyMethod(int id)
{
var x = (from t in context.MyTable
where t.id == id
select new MyClassDatetime()
{
Dates = v.Date
}).Distinct().ToList();
}
}
public class MyClassDatetime : IEquatable<MyClassDatetime>
{
public DateTime? Dates { get; set; }
public bool Equals(MyClassDatetime other)
{
if (other == null) return false;
return (this.Dates.HasValue ? this.Dates.Value.ToShortDateString().Equals(other.Dates.Value.ToShortDateString()) : false);
}
public override bool Equals(object other)
{
return this.Equals(other as MyClassDatetime );
}
public override int GetHashCode()
{
int hashDate = Dates.GetHashCode();
return hashDate;
}
}
}
Have you know how can i make it work properly or other option to do what i need??
Thank you!!
Your implementation of GetHashCode is incorrect for the desired equality semantics. That's because it returns different hash codes for dates that you want to compare equal, which is a bug.
To fix it, change it to
public override int GetHashCode()
{
return Dates.HasValue ? Dates.Value.Date.GetHashCode() : 0;
}
You should also update Equals in the same spirit, it's not a good idea to mess with string representations of dates:
public bool Equals(MyClassDatetime other)
{
if (other == null) return false;
if (Dates == null) return other.Dates == null;
return Dates.Value.Date == other.Dates.Value.Date;
}
Update: As usr very correctly points out, since you are using LINQ on an IQueryable the projection and Distinct call will be translated to a store expression and this code will still not run. To get around that you can use an intermediate AsEnumerable call:
var x = (from t in context.MyTable
where t.id == id
select new MyClassDatetime()
{
Dates = v.Date
}).AsEnumerable().Distinct().ToList();
Thans for reply but it still not solving my problem.
I finally found a way to do it but without using IEquatable.
var x = (from t in context.MyTable
where t.Id == id
select EntityFunctions.CreateDateTime(t.Date.Value.Year, t.Date.Value.Month,t.Date.Value.Day, 0, 0, 0)).Distinct();
=)
Related
filterCollection is a list of int's
I am trying to create a new list from an existing with distinct with Linq
In this cause which is better
filterCollection.Select(filterId => new FilterTable() { FilterId = filtertId }).Distinct().ToList();
OR
filterCollection.Distinct().Select(filterId => new FilterTable() { FilterId = filtertId }).ToList();
I am not sure this is correct.
It depends. It depends on what is in filterCollection, if its Linq-To-Objects or Linq-To-Entities (database driven Linq provider) It depends on the implementation of FilterTable, because if this class does not override Equals and GetHashCode it will not work at all.
Since you project filterId i assume that the it's IEnumerable<int>(or string), then Distinct before the Select will work, it will remove duplicates. But after the Select it might not work because you create new FilterTable instances.
To make it work implement IEquatable<FilterTable> and override Equals+GetHashCode:
public class FilterTable : IEquatable<FilterTable>
{
public int FilterId { get;set; }
public bool Equals(FilterTable other)
{
return FilterId == other?.FilterId;
}
public override bool Equals(object obj)
{
return obj is FilterTable ft && this.Equals(ft);
}
public override int GetHashCode()
{
return FilterId.GetHashCode();
}
}
I have a class named accessoire :
class accessoire
{
public int value1 { get; set; }
public string Value2 { get; set; }
}
then i have a List of that product
List<accessoire> accessoires
And i have an UI where the user pick the product he wants from a DataGridview and when he selected it launch an event that add this item to the list :
private void ProductBrowser_OnItemAdded(Accessoire item)
{
if (Cart.Contains(item))
{
MessageBox.Show("Produit deja ajoutée au panier ! ");
}
else
{
Cart.Add(item);
ProductView.Rows.Add(item.Ref, item.Name, Function.CatName(item.Cat), item.SellPrice, "1", Convert.ToDouble(item.SellPrice) * Convert.ToDouble(item.QtetoSell));
TotalPriceSet();
MessageBox.Show("Produit Ajouté !");
}
}
this doesnt work , but when i change it to :
private void ProductBrowser_OnItemAdded(Accessoire item)
{
var InList = Cart.Find(product => product.Ref == item.Ref);
if (Cart.Contains(InList))
{
MessageBox.Show("Product already in list ! ");
}
else
{
Cart.Add(item);
ProductView.Rows.Add(item.Ref, item.Name, Function.CatName(item.Cat), item.SellPrice, "1", Convert.ToDouble(item.SellPrice) * Convert.ToDouble(item.QtetoSell));
TotalPriceSet();
MessageBox.Show("product added !");
}
}
it works , but i'am still wondering why the first code doesnt work it keep adding that item to the list ? in other way how does the method .Contains()works ? what does it check to know if the item is or the list on not ?
The reason it doesn't find the object in the list is because it is a reference comparison, comparing the instances of the object, not the values. You can have two instances of your class with the same values in their properties, but if you compare them, they are not equal:
accessoire item1 = new accessoire();
item1.value1 = 1;
item1.value2 = "one";
accessoire item2 = new accessoire();
item2.value1 = 1;
item2.value2 = "one";
if(item1 == item2) MessageBox.Show("Same");
else MessageBox.Show("Different");
When you select the item from the list to compare with, you are pulling the specific instance, which does exist in the list.
You need to implement the Equals method of accessoire to properly compare all properties/fields in it. The default implementation of Equals uses ReferenceEquals, which only works if the two instances are in fact the same.
if (Cart.Contains(item))
is matching by equality.
If object.Equals(T) is not satisified, it will fail. That means the smallest change, even whitespace in a string, will return false. You'll also get a false result if you have two instances of the same class. Contains must refer to exactly item.
var InList = Cart.Find(product => product.Ref == item.Ref) is a match by property. This means that other properties of the product/item can all be different, as long as .Ref matches. I presume Ref is a primary key, which is why you're not getting problems in your result where Find() returns the wrong item.
You can get around the difference by overriding Equals for Cart, but I don't recommend it. It can make debugging hell later.
Just implement the equals method
// override object.Equals
public override bool Equals(object obj)
{
//
// See the full list of guidelines at
// http://go.microsoft.com/fwlink/?LinkID=85237
// and also the guidance for operator== at
// http://go.microsoft.com/fwlink/?LinkId=85238
//
if (obj == null || GetType() != obj.GetType())
{
return false;
}
var data = (accessoire)obj;
return this.Ref.Equals(data.Ref);
}
// override object.GetHashCode
public override int GetHashCode()
{
return this.Ref.GetHashCode()
}
You were doing reference compare and the references don't match in your first example, but do in your second. You probably want to do equality comparison. Are the values of both objects the same.
Below is your class implemented with the various methods used for equality comparing. You would just need to modify them to suit your purposes.
// IEquatable<T> provides typed equality comparing
class accessoire : IEquatable<accessoire>
{
public int Value1 { get; set; }
public string Value2 { get; set; }
// you can override Equals.
public override bool Equals(object obj)
{
return this.Equals(obj as accessoire);
}
// this comes from IEquatable<T>
public bool Equals(accessoire other)
{
if (ReferenceEquals(null, other))
{
return false;
}
// return the comparison that makes them equal.
return
this.Value1.Equals(this.Value1) &&
this.Value2.Equals(this.Value2);
}
public override int GetHashCode()
{
unchecked
{
int hash = 37;
hash *= 23 + this.Value1.GetHashCode();
hash *= 23 + this.Value2.GetHashCode();
return hash;
}
}
// allows you to check equality with the == operator
public static bool operator ==(accessoire left, accessoire right)
{
if (ReferenceEquals(left, right))
{
return true;
}
if ((oject)left == null || (object)right == null)
{
return false;
}
return left.Equals(right);
}
public static bool operator !=(accessoire left, accessoire right)
{
return !left.Equals(right);
}
}
This question already has answers here:
LINQ's Distinct() on a particular property
(23 answers)
Closed 20 days ago.
class Program
{
static void Main(string[] args)
{
List<Book> books = new List<Book>
{
new Book
{
Name="C# in Depth",
Authors = new List<Author>
{
new Author
{
FirstName = "Jon", LastName="Skeet"
},
new Author
{
FirstName = "Jon", LastName="Skeet"
},
}
},
new Book
{
Name="LINQ in Action",
Authors = new List<Author>
{
new Author
{
FirstName = "Fabrice", LastName="Marguerie"
},
new Author
{
FirstName = "Steve", LastName="Eichert"
},
new Author
{
FirstName = "Jim", LastName="Wooley"
},
}
},
};
var temp = books.SelectMany(book => book.Authors).Distinct();
foreach (var author in temp)
{
Console.WriteLine(author.FirstName + " " + author.LastName);
}
Console.Read();
}
}
public class Book
{
public string Name { get; set; }
public List<Author> Authors { get; set; }
}
public class Author
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override bool Equals(object obj)
{
return true;
//if (obj.GetType() != typeof(Author)) return false;
//else return ((Author)obj).FirstName == this.FirstName && ((Author)obj).FirstName == this.LastName;
}
}
This is based on an example in "LINQ in Action". Listing 4.16.
This prints Jon Skeet twice. Why? I have even tried overriding Equals method in Author class. Still Distinct does not seem to work. What am I missing?
Edit:
I have added == and != operator overload too. Still no help.
public static bool operator ==(Author a, Author b)
{
return true;
}
public static bool operator !=(Author a, Author b)
{
return false;
}
LINQ Distinct is not that smart when it comes to custom objects.
All it does is look at your list and see that it has two different objects (it doesn't care that they have the same values for the member fields).
One workaround is to implement the IEquatable interface as shown here.
If you modify your Author class like so it should work.
public class Author : IEquatable<Author>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Equals(Author other)
{
if (FirstName == other.FirstName && LastName == other.LastName)
return true;
return false;
}
public override int GetHashCode()
{
int hashFirstName = FirstName == null ? 0 : FirstName.GetHashCode();
int hashLastName = LastName == null ? 0 : LastName.GetHashCode();
return hashFirstName ^ hashLastName;
}
}
Try it as DotNetFiddle
The Distinct() method checks reference equality for reference types. This means it is looking for literally the same object duplicated, not different objects which contain the same values.
There is an overload which takes an IEqualityComparer, so you can specify different logic for determining whether a given object equals another.
If you want Author to normally behave like a normal object (i.e. only reference equality), but for the purposes of Distinct check equality by name values, use an IEqualityComparer. If you always want Author objects to be compared based on the name values, then override GetHashCode and Equals, or implement IEquatable.
The two members on the IEqualityComparer interface are Equals and GetHashCode. Your logic for determining whether two Author objects are equal appears to be if the First and Last name strings are the same.
public class AuthorEquals : IEqualityComparer<Author>
{
public bool Equals(Author left, Author right)
{
if((object)left == null && (object)right == null)
{
return true;
}
if((object)left == null || (object)right == null)
{
return false;
}
return left.FirstName == right.FirstName && left.LastName == right.LastName;
}
public int GetHashCode(Author author)
{
return (author.FirstName + author.LastName).GetHashCode();
}
}
Another solution without implementing IEquatable, Equals and GetHashCode is to use the LINQs GroupBy method and to select the first item from the IGrouping.
var temp = books.SelectMany(book => book.Authors)
.GroupBy (y => y.FirstName + y.LastName )
.Select (y => y.First ());
foreach (var author in temp){
Console.WriteLine(author.FirstName + " " + author.LastName);
}
There is one more way to get distinct values from list of user defined data type:
YourList.GroupBy(i => i.Id).Select(i => i.FirstOrDefault()).ToList();
Surely, it will give distinct set of data
Distinct() performs the default equality comparison on objects in the enumerable. If you have not overridden Equals() and GetHashCode(), then it uses the default implementation on object, which compares references.
The simple solution is to add a correct implementation of Equals() and GetHashCode() to all classes which participate in the object graph you are comparing (ie Book and Author).
The IEqualityComparer interface is a convenience that allows you to implement Equals() and GetHashCode() in a separate class when you don't have access to the internals of the classes you need to compare, or if you are using a different method of comparison.
You've overriden Equals(), but make sure you also override GetHashCode()
The Above answers are wrong!!!
Distinct as stated on MSDN returns the default Equator which as stated The Default property checks whether type T implements the System.IEquatable interface and, if so, returns an EqualityComparer that uses that implementation. Otherwise, it returns an EqualityComparer that uses the overrides of Object.Equals and Object.GetHashCode provided by T
Which means as long as you overide Equals you are fine.
The reason you're code is not working is because you check firstname==lastname.
see https://msdn.microsoft.com/library/bb348436(v=vs.100).aspx and https://msdn.microsoft.com/en-us/library/ms224763(v=vs.100).aspx
You can achieve this several ways:
1. You may to implement the IEquatable interface as shown Enumerable.Distinct Method or you can see #skalb's answer at this post
2. If your object has not unique key, You can use GroupBy method for achive distinct object list, that you must group object's all properties and after select first object.
For example like as below and working for me:
var distinctList= list.GroupBy(x => new {
Name= x.Name,
Phone= x.Phone,
Email= x.Email,
Country= x.Country
}, y=> y)
.Select(x => x.First())
.ToList()
MyObject class is like as below:
public class MyClass{
public string Name{get;set;}
public string Phone{get;set;}
public string Email{get;set;}
public string Country{get;set;}
}
3. If your object's has unique key, you can only use the it in group by.
For example my object's unique key is Id.
var distinctList= list.GroupBy(x =>x.Id)
.Select(x => x.First())
.ToList()
You can use extension method on list which checks uniqueness based on computed Hash.
You can also change extension method to support IEnumerable.
Example:
public class Employee{
public string Name{get;set;}
public int Age{get;set;}
}
List<Employee> employees = new List<Employee>();
employees.Add(new Employee{Name="XYZ", Age=30});
employees.Add(new Employee{Name="XYZ", Age=30});
employees = employees.Unique(); //Gives list which contains unique objects.
Extension Method:
public static class LinqExtension
{
public static List<T> Unique<T>(this List<T> input)
{
HashSet<string> uniqueHashes = new HashSet<string>();
List<T> uniqueItems = new List<T>();
input.ForEach(x =>
{
string hashCode = ComputeHash(x);
if (uniqueHashes.Contains(hashCode))
{
return;
}
uniqueHashes.Add(hashCode);
uniqueItems.Add(x);
});
return uniqueItems;
}
private static string ComputeHash<T>(T entity)
{
System.Security.Cryptography.SHA1CryptoServiceProvider sh = new System.Security.Cryptography.SHA1CryptoServiceProvider();
string input = JsonConvert.SerializeObject(entity);
byte[] originalBytes = ASCIIEncoding.Default.GetBytes(input);
byte[] encodedBytes = sh.ComputeHash(originalBytes);
return BitConverter.ToString(encodedBytes).Replace("-", "");
}
The Equal operator in below code is incorrect.
Old
public bool Equals(Author other)
{
if (FirstName == other.FirstName && LastName == other.LastName)
return true;
return false;
}
NEW
public override bool Equals(Object obj)
{
var other = obj as Author;
if (other is null)
{
return false;
}
if (FirstName == other.FirstName && LastName == other.LastName)
return true;
return false;
}
Instead of
var temp = books.SelectMany(book => book.Authors).Distinct();
Do
var temp = books.SelectMany(book => book.Authors).DistinctBy(f => f.Property);
This is how the custom object is defined:
public class AccountDomain
{
public string MAILDOMAIN { get; set; }
public string ORG_NAME { get; set; }
}
This is how I am populating the List of objects:
List<AccountDomain> mainDBAccountDomain = mainDB.GetAllAccountsAndDomains();
List<AccountDomain> manageEngineAccountDomain = ManageEngine.GetAllAccountsAndDomains();
This code works fine - if I look at the locals windows I can see a List of Objects in both mainDBAccountDomain and manageEngineAccountDomain.
I'm struggling with the next bit, ideally I want a new list of type AccountDomain that contains all entries that are in mainDBAccountDomain and not ManageEngineAccountDomain
Any help greatly appreciated, even if it's just a pointer in the right direction!
I want a new list of type AccountDomain that contains all entries that are in mainDBAccountDomain and not ManageEngineAccountDomain
It's very simple with linq to objects, it's exactly what the Enumerable.Except function does:
var result = mainDBAccountDomain.Except(manageEngineAccountDomain).ToList();
You can pass a comparer to the Except function if you need something different from reference equality, or you could implement Equals and GetHashCode in AccountDomain (and optionally implement IEquatable<AccountDomain> on top of these).
See this explanation if you need more details about comparers.
Here's an example:
public class AccountDomainEqualityComparer : IEqualityComparer<AccountDomain>
{
public static readonly AccountDomainEqualityComparer Instance
= new AccountDomainEqualityComparer();
private AccountDomainEqualityComparer()
{
}
public bool Equals(AccountDomain x, AccountDomain y)
{
if (ReferenceEquals(x, y))
return true;
if (x == null || y == null)
return false;
return x.MAILDOMAIN == y.MAILDOMAIN
&& x.ORG_NAME == y.ORG_NAME;
}
public int GetHashCode(AccountDomain obj)
{
if (obj == null)
return 0;
return (obj.MAILDOMAIN ?? string.Empty).GetHashCode()
^ (397 * (obj.ORG_NAME ?? string.Empty).GetHashCode());
}
}
Then, you use it like this:
var result = mainDBAccountDomain.Except(manageEngineAccountDomain,
AccountDomainEqualityComparer.Instance)
.ToList();
I have a class that was previously being used in a HashSet. This has now been changed so that the class is now used in a SortedSet, but the equality test no longer works as it did. I believe this it uses the CompareTo function for both sorting and comparing, and this is by design.
Anyone have any ideas, other than performing my own duplicate checks?
public sealed class DatedID : IEquatable<DatedID>, IComparable
{
readonly DateTime _added;
readonly int _ID;
public DateTime Added
{
get { return _added; }
}
public int ID
{
get { return _ID; }
}
public DatedID(int id)
: this(id, DateTime.Now) {}
public DatedID(int id, DateTime added)
{
id.ThrowDefault("id");
added.ThrowDefault("added");
_ID = id;
_added = added;
}
// Compare
int IComparable.CompareTo(object obj)
{
var other = (DatedID)obj;
// Newest => oldest
return this.Added > other.Added ? -1 : this.Added < other.Added ? 1 : 0;
}
// Equals
public bool Equals(DatedID other)
{
if (other == null) return false;
return this.ID == other.ID;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
var di = obj as DatedID;
return di == null ? false : Equals(di);
}
public override int GetHashCode()
{
return ID.GetHashCode();
}
}
If you mean you need to be able to handle multiple values with different IDs but the same DateTime, you could include that in your CompareTo implementation:
// TODO: Implement IComparable<DatedID> as well :)
int IComparable.CompareTo(object obj)
{
var other = (DatedID)obj;
int dateComparison = other.Added.CompareTo(this.Added);
return dateComparison != 0
? dateComparison
: _ID.CompareTo(other._ID);
}
If you mean you don't want to be able to add multiple values with the same ID but different dates, then you can't achieve that with SortedSet. In SortedSet, the only measure of equality is if the comparison returns 0.