Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Entity:</p> <pre><code>public class Character { public string Name; } </code></pre> <p>Comparer</p> <pre><code>public class CharacterComparer : IEqualityComparer&lt;Character&gt; { public bool Equals(Character x, Character y) { if (ReferenceEquals(x, y)) { return true; } if (ReferenceEquals(x, null) || ReferenceEquals(y, null)) { return false; } // Ignore case and culture return string.Compare(x.Name, y.Name, StringComparison.InvariantCultureIgnoreCase) == 0; } public int GetHashCode(Character obj) { if (obj == null || string.IsNullOrEmpty(obj.Name)) { return 0; } return obj.Name.GetHashCode(); } } </code></pre> <p>use HashSet.</p> <pre><code> HashSet&lt;Character&gt; characters1 = new HashSet&lt;Character&gt;(new CharacterComparer()); characters1.Add(new Character {Name = "Alex"}); characters1.Add(new Character { Name = "Peter" }); characters1.Add(new Character { Name = "John" }); HashSet&lt;Character&gt; characters2 = new HashSet&lt;Character&gt;(new CharacterComparer()); characters2.Add(new Character { Name = "John" }); characters2.Add(new Character { Name = "Jenny" }); characters2.IntersectWith(characters1); Console.WriteLine("Existing count: " + characters2.Count); </code></pre> <p>Output:</p> <pre><code>Existing count: 1 </code></pre> <p><strong>EDIT 1</strong></p> <p>And do not add new item in session's list:</p> <pre><code> listOfCharacters.Add(new Character() { name = txtName.Text}); </code></pre> <p>You've already added new Character to list and then tries to find it in the same list. You should use separate lists. Session's list (existing Characters) and new List with Characters to be added.</p> <p>add </p> <pre><code>using System.Linq; </code></pre> <p>and iterate through new list to search existing users in Session's list </p> <pre><code>// characters2 = created new list with Characters to be added foreach (var item in characters2) { Character character = item; // characters1 = obtained from sessions bool contains = characters1.Contains(character, new CharacterComparer()); if (contains) { Console.WriteLine("Character {0} exists", character.Name); } } </code></pre>
    singulars
    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.
    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