C# Hashset key value, unique, problem

Markus Freitag 3,786 Reputation points
2022-02-23T17:06:42.493+00:00

Hello,

I have a list that has two pages /sides. Side 1 and side 2.
If the condition of an item is bad, it can no longer become good.

How can I solve this well? Or is Hashset not the right container...?
The hashset I can not assign anything.
The key is Row/Column.
Once bad always bad!

Thanks for tips!

177208--tableview4.png

HashSet<Tuple<string, bool>> hsGoodBad = new HashSet<Tuple<string, bool>>();  
var exist = hsGoodBad.Where(r => r.Item1 == Index).FirstOrDefault();  
  
  
if (exist != null)  
{  
   if (exist.Item2 == true && p.POSITION.BAD.value == 1)  
       exist.Item2 = false; // not possible!  
}  
  
  
Side 1               Side 2         Whole  
Row/Col State       Row/Co l State     
1/1     true        1/1  true    true  
1/2     true        1/2  true    true  
1/3     false       1/3  false   false  
1/4     true        1/4  true    true  
1/5     true        1/5  true    true  
1/6     true        1/6  true    true  
1/7     true        1/7  false   false  
1/8     true        1/8  true    true  
1/9     true        1/9  true    true  
1/10    true        1/10 true    true  
2/1     true        2/1  true    true  
2/2     true        2/2  true    true  
2/3     true        2/3  true    true  
2/4     true        2/4  true    true  
2/5     false       2/5  true    false  
2/6     true        2/6  true    true  
2/7     true        2/7  true    true  
2/8     true        2/8  true    true  
2/9     true        2/9  true    true  
2/10    true        2/10 true    true  
  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,564 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,491 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,946 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 65,556 Reputation points
    2022-02-24T18:11:38.43+00:00

    you are not storing the side, only the unique combinations of the sides:

    1/1 true //side one
    1/1 false //side two
    1/1 true //side two

    and

    1/1 true //side one
    1/1 false //side one
    1/1 true //side two

    generate the same hashset. you can not tell the difference.

    1/1 true
    1/1 false

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 65,556 Reputation points
    2022-02-23T19:11:19.427+00:00

    hashsets have unique values so dups will be eliminated. the bad is any false item 2, good is item2 is true, with no false row

            var hs = new HashSet<Tuple<string,bool>> 
            {
                new Tuple<string,bool>("1/1",true),
                new Tuple<string,bool>("1/1",true),
                new Tuple<string,bool>("1/2",true),
                new Tuple<string,bool>("1/2",false),
            };
    
            var good = hs.Where(t1 => t1.Item2 && !hs.Any(t2 => t2.Item1 == t1.Item1 && !t2.Item2));
            var bad = hs.Where(t => t.Item2 == false);
    
            Console.WriteLine("Good");
            foreach (var t in good) Console.WriteLine(t);
    
            Console.WriteLine("Bad");
            foreach (var t in bad) Console.WriteLine(t);
    
    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 65,556 Reputation points
    2022-02-24T16:24:30.587+00:00

    it not only true, hashsets only contain unique values. if you add the same value twice, as types are value type, they are compared property by property.

    in your example:

     var hs = new HashSet<Tuple<string, bool>>
     {
      new Tuple<string,bool>("1/1",true), 
      new Tuple<string,bool>("1/1",true),
      new Tuple<string,bool>("1/2",true),
      new Tuple<string,bool>("1/2",false),
      new Tuple<string,bool>("1/2",true),
      new Tuple<string,bool>("1/2",false),
      new Tuple<string,bool>("1/2",true)
     };
    

    the dups are removed, and it is the same as:

     var hs = new HashSet<Tuple<string, bool>>
     {
      new Tuple<string,bool>("1/1",true), 
      new Tuple<string,bool>("1/2",true),
      new Tuple<string,bool>("1/2",false),
      } ;
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.