score:104
forget iequalitycomparer and just use linq directly:
employeecollection.groupby(x => new{x.fname, x.lname}).select(g => g.first());
score:-1
public int gethashcode(employe obj)
{
return obj.gethashcode();
}
for this method, return a hashcode of the properties that you are comparing for equality, instead of the object itself. comparing the hashcode of the objects will always be false
, so your list will never be filtered for duplicates.
score:0
also it looks like your comparing by reference instead of content, hence the compare function doesn't work.
change it to use .equals() instead of == and it should work. example below:
#region iequalitycomparer<pcf> members
public bool equals(employe x, employe y)
{
if (x.fname.equals(y.fname) && x.lname.equals(y.lname))
{
return true;
}
return false;
}
public int gethashcode(employe obj)
{
return obj.gethashcode();
}
#endregion
score:3
the hashcode implementation is not correct:
public override int gethashcode()
{
return 13 * fname.gethashcode() + 7 * lname.gethashcode();
}
score:5
you need to override gethashcode
method in your employee. you haven't done this. one example of a good hashing method is given below: (generated by resharper)
public override int gethashcode()
{
return ((this.fname != null ? this.fname.gethashcode() : 0) * 397) ^ (this.lname != null ? this.lname.gethashcode() : 0);
}
now after distinct
is called, foreach loop prints:
abc def
lmn def
in your case you are calling object's class gethashcode
, which knows nothing about internal fields.
one simple note, morelinq contains distinctby extension method, which allows you to do:
ienumerable<employe> coll =
employeecollection.distinctby(employee => new {employee.fname, employee.lname});
anonymous objects have correct implementation for both gethashcode
and equals
methods.
score:5
here is a good tutorial
public int gethashcode(employe obj)
{
return obj.fname.gethashcode() ^ obj.lname.gethashcode();
}
Source: stackoverflow.com
Related Query
- How to remove duplicates from collection using IEqualityComparer, LinQ Distinct
- How to remove duplicates from an Array using LINQ
- How to remove duplicates from SQLite DB - using ENtity and LINQ
- How to remove duplicates from a List of List using Linq
- How do I remove items from generic list, based on multiple conditions and using linq
- How to remove characters from a string using LINQ
- How to get Distinct Values from List(Of T) using Linq
- using LINQ how can i concatenate string properties from itesm in a collection
- Using LINQ how do I create a List of one particular field of an entity from a collection entities
- How to remove duplicate combinations from a List<string> using LINQ
- How to get strongly-typed collection from XML using Linq
- How to aggregate the value from DISTINCT row using LINQ
- How to select distinct employees with LINQ based on ID from an employee collection where employee 's salary is 2nd highest?
- Filtering duplicates from a type collection using LINQ
- How can I get the top three players and their high scores from a collection using linq and lambdas
- How do I get the first value from this collection using Linq to Entities?
- How to remove substring from all strings in a list in C# using LINQ
- How to find overlapping in collection using LINQ ( not duplicates but to find overlaps)
- Remove items from collection using linq
- How to get the second repeated item from a collection of objects using LINQ to object
- How to get the distinct record name and its each record count to bind to gridview in C# using Linq from SQL Server
- How to remove an item from ListView using LINQ in C#
- How do i return a certain max number of items from a collection using linq
- Remove all entries from collection, which are found in different collection using linq
- how to remove objects from list by multiple variables using linq
- How to remove any value from Dictionary<string,List<string>> using LINQ
- How to SUM the value from DISTINCT row using LINQ
- Remove duplicates from a List<Object> using LINQ
- How to Filter Child Collection from each Parent Entity in the Parent Collection using Linq
- Using LINQ and EF, how to remove values from database where not in list of items
More Query from same tag
- Linq Grouping with Include
- How to use DateAdd function in Linq?
- Update using Linq Query
- MongoDB .NET driver and text search
- How to write a LINQ join for two classes in ASP.NET MVC4
- Entity Framework 4.0 only include some related data
- What is the Java 8 Stream API equivalent for LINQ Join?
- how to sum and group data in linq
- Compiling Error with LINQ Sorting Code Using List<T>
- C# web API linq to SQL List object
- Where clause in Linq in List c#
- From Sql to Linq-toSql
- LINQ request many-to-many
- LINQ without using cycle
- Generic All Controls Method
- C# Sort items in a list, withing the list, that satisfy a condition
- How to convert List<Company> to List<ICompany>
- how to select records from multiple table with max count value from one table using Linq in Asp.net MVC C#
- ASP.NET MVC how to create a model which pulls in data from 2 tables, to display in 1 view
- LINQ Is it possible to get a method name without a return type via LINQ expression trees?
- How to use Date function in LINQ to entities?
- Convert where clause containing select to Linq
- Parse nested XML using XDocument
- LINQ to Object basic Remove
- how to perform groupby in linq on DataTable inside vb code?
- Finding the biggest TestId in the list
- Update like Sub query using Linq to Sql
- Cannot get data using LINQ in MVC
- Updating a list based on another list
- Read and Remove invalid characters from xml outside xml elements in C# Linq to Xml