osyris zosar

osyris zosar

  • NA
  • 289
  • 26.8k

Lambda problem I can not seem to figure out.

Jan 11 2021 1:57 AM
I really have no idea how to figure this out, please help. 
 
I have this filter button that is suppose to filter a listbox based on what is filled in a textbox and a list that is used to add the names the listbox
 
this is my code:
  1. public void FilterButton_Click(object sender, RoutedEventArgs e)  
  2.         {  
  3.   
  4. //2 filter examples I used  
  5.   
  6. bool Agemin = int.TryParse(AgeBoxMin.Text, out int AgeMi);  
  7.   
  8. //first filter  
  9.  if (Agemin && AgeBoxMin.Text.Length != 0 && (AgeBoxMax.Text.Length == 0 || Agemax != true))  
  10.             {   
  11.                 filtercount++;  
  12.                 int T = 0; while (T < AgeListNumb.Count)  
  13.                 {  
  14.                     if (AgeListNumb[T] > AgeMi)  
  15.                     {  
  16.                         IndexAge.Add(T);  
  17.                         TotalFilter.Add(T);  
  18.                     }T++;}}  
  19. //second filter  
  20. if (JobTextBox.Text.Length != 0)  
  21.             {      filtercount++;  
  22.                 int P = 0; while (P < JobList1.Count)  
  23.                 {  if (JobList1[P].ToLower().Contains(JobTextBox.Text.ToLower()))  
  24.                     {  
  25.                         IndexJob.Add(P);  
  26.                         TotalFilter.Add(P);  
  27.                     }P++;}}  
  28.   
  29. // the filter system:  (probable the problem)
  30. var  matches = TotalFilter.GroupBy(x => x).Where(x => x.Count() == filtercount).Select(x => x.Key).ToList();  
  31.   
  32.   
  33. // applying filter to Listbox  
  34. if (filtercount != 0 && matches.Any())  
  35.             {  
  36.                 AccountNamesList.Items.Clear();  
  37.                                   
  38.                 foreach (var I in matches)  
  39.                     AccountNamesList.Items.Add(Listboxlist[I]);  
  40.             }  
  41.   
  42. //(Listboxlist contains random names, those names are created with a function)  
  43.   
  44. }  
 The first time i run it it runs perfect just as i want it,
But the second time i use the above code it doubles  matches
 
I have looked up what happend with the matches list:
  1. messageBox.Show(Matches.Aggregate("Matches indexes : ", (s1, s2) => s1 + " , " + s2);  
If i use the first filter and i allow all items in Listboxlist
to pass( filling in 0 in AgeBoxMin(textbox))
it gives the output:
 
(If listboxlist would contain 6 items )
1,2,3,4,5,6  
 
If i do it again 
I get the output: 1,2,3,4,5,6,7,8,9,10,11,12
 
for some reason it doubles it and makes the entire program crash
because list box only contains 6 items not 12.
 
Maybe somehow it stores all the items in  "var matches" and adds it all up.
 

Answers (5)