Home > Net >  Filter object ListCollectionView using object's nested SerializedDictonary in C#
Filter object ListCollectionView using object's nested SerializedDictonary in C#

Time:02-10

I would like to filter ListCollectionView using SerializableDictionary which is nested in objects. The filter will check values for the specified (hardcoded) key.

Object (only pseudocode to avoid too much code):

MasterRecord{
   MasterName string,
   AdditionalAttributes SerializableDictionary
   }

Below is my current simple filter using MasterName(string):

    public string Filter
    {
        get
        {
            return m_Filter;
        }
        set
        {
            if (value != m_Filter)
            {
                m_Filter = value;

                ListCollectionView masterRecords = (ListCollectionView)CollectionViewSource.GetDefaultView(MasterRecordCollection);
                if (!string.IsNullOrEmpty(m_Filter))
                {
                    masterRecords.Filter = (item) => { return (item as MasterRecord).MasterName.StartsWith(m_Filter) ? true : false; };

                }
                else
                    masterRecords.Filter = null;

                masterRecords.Refresh();
            }
        }
    }

Sample:

sample data

How to modyfi my current method and filter by Value (using .Filter() and .StartWith(), for exmaple 'COCA-COLA') AdditionalAttributes(SerializableDictionary) when (this part should be hardcoded) Key == 'c_ProductName' ?

CodePudding user response:

Solution (thanks to @jdweng):

masterRecords.Filter = (item) => { return (item as MasterRecord).AdditionalAttributes.Any(y => (y.Key == "c_ProductName") && (y.Value.StartsWith(m_Filter)));}; 
  • Related