Good day !

There is an array in which the CheckBox written. Some of them isChecked , some !isChecked .

I need to select all checkbox that isChecked and remove from this array all checkBox which have .Tag == "равен чему либо".

I tried this:

  var myTestArray = _statusCheckBoxes.Where(x => !x.Tag.Equals("New") && !x.Tag.Equals("Active") && !x.Tag.Equals("Activating")); // тут я хотел выбрать все чексбоксы кроме тех, у которых .Tag равен (New, Active, Activating) var xxx = myTestArray.Where(x => x.IsChecked); // тут я выбрал из тех, которые мне нужны isSelected 

But my request to select the necessary elements does not work. Tell me how this can be done or what do I need to change in this code?

Thank !

UPD.

 private void checkTestMethod() { var myxxx = _statusCheckBoxes.Where( x => (x.Tag as string) != "New" && (x.Tag as string) != "Active" && (x.Tag as string) != "Activating"); var xxx = myxxx.Where(x => x.IsChecked); foreach (var item in xxx) { if (item.IsChecked) MessageBox.Show("Bla bla bla"); } } 
  • one
    @Raider indicated an error, deleted the answer. I think further. - VladD
  • And what will this give out? pastebin.com/ZBDstBrF - VladD
  • @VladD in this variable all .Tags from my array - kxko
  • Well, yes, and you can copy-paste somewhere on pastebin a specific value? - VladD
  • I really don't understand this: (itm.Tag as string)! = "New" <- .Tag now has "New" in itself, but the compiler says that the condition has true, although it should be false in theory - kxko

1 answer 1

Old reply deleted, new reply:


An investigation in the comments and in the chat showed that the objects in the Tag were not of type string . Therefore, for comparison, I needed the following code:

 x.Tag.ToString() != "New" 

etc. Also, the checks can be combined:

 .Where(x => x.IsChecked && x.Tag.ToString() != "New" && x.Tag.ToString() != "Active" && x.Tag.ToString() != "Activating") 

If Tag can be equal to null , then another question mark is needed:

 (x.Tag?.ToString()) != "New" 

etc.

  • Thanks for the answer ! I did what you said, but it still returns all the elements = ( - kxko
  • @kxxko: Hmm, strange, it should work. Can you show more code? Maybe there is a problem somewhere else? - VladD
  • @kxxko: What do you do next with xxx ? - VladD
  • I run foreach and do my checks on each element. I also thought it would work. - kxko
  • one
    @VladD You confuse object.Equals and object.ReferenceEquals . The Equals method on the line is overridden, and it will perform the comparison by value, not by reference. - Raider