Skip to content

Instantly share code, notes, and snippets.

@MRdNk
Last active December 12, 2015 00:18
Show Gist options
  • Save MRdNk/4682346 to your computer and use it in GitHub Desktop.
Save MRdNk/4682346 to your computer and use it in GitHub Desktop.

An alternative to doing C# List Delegates (https://gist.github.com/4654896) is to use a Predicate / Lambda Expression...

private List<KeyValueBool<TKey, TValue>> _list;

public Boolean Exists (TKey ID) {
  return _list.Exists (item => EqualityComparer<TKey>.Default.Equals (item.Key, ID));
}

public TValue GetValue (TKey ID) {
  return _list.Find (item => EqualityComparer<TKey>.Default.Equals (item.Key, ID)).Value;
}

public List<KeyValueBool<TKey, TValue>> GetDisplayable () {
  return _list.FindAll (item => item.Boolean == true);
}

KeyValueBool (class) for reference

  public class KeyValueBool<TKey, TValue> {

    public TKey Key {
      get;
      set;
    }
    public TValue Value {
      get;
      set;
    }
    public Boolean Boolean {
      get;
      set;
    }

    public KeyValueBool (TKey key, TValue value, Boolean b) {
      this.Key = key;
      this.Value = value;
      this.Boolean = b;
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment