Advertisement
Guest User

Untitled

a guest
Sep 6th, 2014
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Test
  11. {
  12. public class GroupKeyList<T> : ObservableCollection<T>, INotifyPropertyChanged
  13. {
  14. public string Key { get; private set; }
  15.  
  16. // Declare the event
  17. public new event PropertyChangedEventHandler PropertyChanged;
  18.  
  19. // Create the OnPropertyChanged method to raise the event
  20. protected void OnPropertyChanged(string name)
  21. {
  22. PropertyChangedEventHandler handler = PropertyChanged;
  23. if (handler != null)
  24. {
  25. handler(this, new PropertyChangedEventArgs(name));
  26. }
  27. }
  28.  
  29. public delegate string GetKeyDelegate(T item);
  30.  
  31. public static ObservableCollection<GroupKeyList<T>> CreateGroupList(ObservableCollection<T> items, GetKeyDelegate getKey)
  32. {
  33. ObservableCollection<GroupKeyList<T>> new_group_list = new ObservableCollection<GroupKeyList<T>>(); // create the list
  34.  
  35. foreach (T item in items)
  36. {
  37. string key = getKey(item); // get key
  38.  
  39. // search to see if the key has been added to the group
  40. GroupKeyList<T> new_item = new_group_list.FirstOrDefault(delegate(GroupKeyList<T> t) { return t.Key == key; });
  41.  
  42. if (new_item == null)
  43. {
  44. new_item = new GroupKeyList<T>(); // create the group
  45. new_item.Key = key; // set the key
  46. new_group_list.Add(new_item); // add the new group
  47. new_item.Add(item); // add the item to the new group
  48. }
  49. else
  50. {
  51. new_item.Add(item); // add the item to the existing group (matching)
  52. }
  53. }
  54.  
  55. return new_group_list;
  56.  
  57.  
  58. }
  59.  
  60. public T FindMatch(string pattern, GetKeyDelegate getKey)
  61. {
  62. Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
  63.  
  64. foreach (T item in this.Items)
  65. {
  66. string key = getKey(item);
  67. Match match = rgx.Match(key);
  68. if (match.Success)
  69. return item;
  70. }
  71.  
  72. return default(T);
  73. }
  74.  
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement