Search code examples
c#wpfdata-bindingobservablecollectionicollectionview

ICollectionView Filter issue wpf c#


I am trying to filter the ICollection but the filter is not getting applied and instead it gives me all records. is there any issue in the predicate ? so there are two methods where the filter is applied on collection. First it filters records for SW_Version and then in the second filter it filters for matching id. basically i am implementing search functionality.

   public void Updateswlist()
    {
        CRmappings2 = new ObservableCollection<SPFetchCREntity>(crentities.ToList());
        AllCRSP = CollectionViewSource.GetDefaultView(CRmappings2);
        SearchMU = SelectedSW.SW_Version;
        AllCRSP.Filter = obj =>
        {
            SPFetchCREntity entity = obj as SPFetchCREntity;
            return obj != null && entity.SW_Version == SearchMU.ToString();
        };
        AllCRSP.Refresh();

2nd Filter

        public void searchMUID()
    {
        AllCRSP.Filter = obj =>
        {
            SPFetchCREntity entity = obj as SPFetchCREntity;
            return obj != null && entity.MU_Identifier == Mupass.ToString();
        };
        AllCRSP.Refresh();
    }

Solution

  • The second filter overwrites the first one. If you want to be able to filter by both properties, you need to include both conditions in your predicate:

    public void searchMUID()
    {
        string Mupass = "";
        AllCRSP.Filter = obj =>
        {
            SPFetchCREntity entity = obj as SPFetchCREntity;
            return obj != null && entity.SW_Version == SearchMU.ToString() && entity.MU_Identifier == Mupass.ToString();
        };
        AllCRSP.Refresh();
    }