2
\$\begingroup\$

I have a scenario wherein I have to filter a collection in a PagedCollectionView. The filter criteria are Name and status which are checkboxes with Enabled and Disabled labels. If Enabled is checked it should filter out the list with all enabled rules and vice-versa. I have given it a try, but I believe this isn't the best way of doing it.

     private PagedCollectionView _autoDepRuleList;
    public PagedCollectionView AutoDepRuleList
    {
        get
        {
            return this._autoDepRuleList;
        }
        set
        {
            this._autoDepRuleList = value;
            RaisePropertyChanged("AutoDepRuleList");
        }
    }        

    private bool _isEnabled;
    public bool IsEnabled
    {
        get
        {
            return this._isEnabled;
        }
        set
        {
            this._isEnabled = value;
            RaisePropertyChanged("IsEnabled");
        }
    }

    private bool _isDisabled;
    public bool IsDisabled
    {
        get
        {
            return this._isDisabled;
        }
        set
        {
            this._isDisabled = value;
            RaisePropertyChanged("IsDisabled");
        }
    }

    private string _name;
    public string Name
    {
        get
        {
            return this._name;
        }
        set
        {
            this._name = value;
            RaisePropertyChanged("Name");
        }
    }

     public void Search()
    {
        var tempList = new AutoDeploymentRuleList();
        var filterList = new AutoDeploymentRuleList();

        foreach (AutoDeploymentRule item in this.AutoDepRuleList)
        {
            tempList.Add(item);
        }
        if (IsEnabled && IsDisabled)
        {
            foreach (var item in tempList)
            {
                filterList.Add(item);
            } 
        }
        else if (IsEnabled)
        {
            foreach (var item in tempList.Where(r => r.Status.Equals("Enabled")))
            {
                filterList.Add(item);
            } 
        }
        else if (IsDisabled)
        {
            foreach (var item in  tempList.Where(r => r.Status.Equals("Disabled")))
            {
                filterList.Add(item);
            }                
        }
        else
        {
            foreach (var item in tempList)
            {
                filterList.Add(item);
            } 
        }
    this.AutoDepRuleList = new PagedCollectionView(filterList.Where(r => r.Name.ToLower().Contains(this.Name.ToLower())));

Now the idea is to do away with the above block and go for the below logic. I did it for the Name property, but I'm not sure how I can do it for the Status property which can be anything either Enabled or Disabled.

    this.AutoDepRuleList.Filter = new System.Predicate<object>(o =>
    {
        AutoDeploymentRule rule = (AutoDeploymentRule)o;

        if (rule.Name.ToLower().IndexOf(this.Name.Trim().ToLower()) >= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    });          
}

How can I optimize it by removing those redundant where clauses and putting the entire thing under Predicate<object>?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

If performance is your concern, won't boxing and unboxing the variable unnecessarily add an overhead? Why would you want to do that?

Wouldn't this be much easier?

this.AutoDepRuleList //if this is queryable/enumerable, or convert first
.Where(r => (isDisabled && r.Status.Equals("Disabled")) || 
             (isEnabled && r.Status.Equals("Enabled")) ||
              (!isDisabled && !isEnabled))
//either ToList() if you just need to assign to another list, or ForEach if you want
//to iterate over list and do something
    .ForEach(filterList.Add);

Update: had forgotten the condition where both isDisabled and isEnabled were false.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ you could also go var filterList = [Roopesh's linq].ToList() \$\endgroup\$ Sep 25, 2012 at 17:05
  • \$\begingroup\$ yup.. any idea which is more efficient? Anyways I put a foreach there so it'll work if the method has some additional logic as well instead of just .Add(). \$\endgroup\$ Sep 25, 2012 at 17:09
  • \$\begingroup\$ hi @RoopeshShenoy, thanks for the post; as AutoDepRuleList is of type PagedCollectionView we can't apply linq methods to it directly, this.AutoDepRuleList.SourceCollection.OfType<AutoDeploymentRule>().Where(r => (IsDisabled && r.Status.Equals("Disabled")) || (IsEnabled && r.Status.Equals("Enabled")) || (!IsDisabled && !IsEnabled)); \$\endgroup\$
    – noor syyed
    Sep 26, 2012 at 6:34
  • \$\begingroup\$ Great if it works for you! \$\endgroup\$ Sep 26, 2012 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.