Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to change Distinct funcationality within .NET?
    primarykey
    data
    text
    <p>I have an issue that I am sure someone out here have solved easier than what I am thinking to do. I have a list that have name and number. Name is required and could be duplicate and number could only be one, but not required.</p> <pre><code>|name|number| |A |2 | |A | | |B | | |C | | |C | | |D |4 | ------------- </code></pre> <p>At this moment I am performing a list.Distinct() on the name part, but it does not care about the number section. </p> <pre><code>newlist = oldlist.Distinct().ToList(); </code></pre> <p>If one name that is duplicate has a number I want to keep the one with the number, which is not happening. The case of two same names and two different numbers will not happen. Any ideas?</p> <p>One more thing: At this moment I don't care about sorting since distinct take care of that.</p> <p>I am adding all the code for better visibility of the StackOverflow Team:</p> <pre><code> class _ClientComparer : IEqualityComparer&lt;_Client&gt; { #region IEqualityComparer&lt;_Client&gt; Members public bool Equals(_Client x, _Client y) { if ((x.ClientNumber != 0) &amp;&amp; (y.ClientNumber != 0))//Both clients with numbers if (x.ClientNumber == y.ClientNumber)//both clients number the same then same clients. return true; else //if not the same they are different return false; else if (x.ClientName == y.ClientName) return true; else return false; } public int GetHashCode(_Client obj) { if (obj.ClientNumber != 0) return obj.ClientNumber.GetHashCode(); else return obj.ClientName.GetHashCode(); } </code></pre> <p>Above the IEqualityComparer implementation and below the Distinct Call.</p> <pre><code> public List&lt;_Client&gt; CollectAllClients() { List&lt;_Client&gt; ClientList = new List&lt;_Client&gt;(); while (this.Read()) { if (GetClientNumber() != 0) ClientList.Add(CreateClientInstance()); else ClientList.AddRange(CreateClientsInstance()); } ClientList = ClientList.Distinct&lt;_Client&gt;(new _ClientComparer()).ToList&lt;_Client&gt;() ; return ClientList; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload