Object Search in BindingList

I have an Asynchronous BindingList that contains objects that are processed in a workflow and bound to a BindingSource in the main UI thread with a BindingSource associated with a DataGridView.

Is it possible to find objects in my BindingList without repeating in the list?

I looked under the LINQ hood, and it's basically a sugar covered foreach loop. Also from my understanding, if I implement IBindingList.Find () this is nothing more than a loop ...

I "tried" to synchronize / match my BindingList with a Dictionary that reflects my BindingList, and use the dictionary for localized objects and pass the results (index) to my BindingList, but this does not work, because adding and removing objects is too much, and I don’t I can hold everything.

This is a high-performance application that deals with high-frequency data in real time from the stock market. Therefore, I cannot go through the BindingList, it is too inefficient.

Can someone please give me some tips and / or solutions.

+5
source share
1 answer

So, some quick search-binding list ... Here's the one I prepared earlier.

This is the sync / map approach you were talking about. I used this before for fast ticking data, where the main bottleneck was looking for items in the list. I believe that I have considered all the methods necessary for synchronization or "organization." You can add a test for AddRange - I don't have a decompiler, I'm not sure if it just calls InsertItem.

, , , , .

, , BindingList, , FastFind.

public class FastLookupBindingList<TKey, TVal> : BindingList<TVal>
{
    private readonly IDictionary<TKey, TVal> _dict = new Dictionary<TKey, TVal>();
    private readonly Func<TVal, TKey> _keyFunc;

    public FastLookupBindingList(Func<TVal, TKey> keyFunc)
    {
        _keyFunc = keyFunc;
    }

    public FastLookupBindingList(Func<TVal, TKey> keyFunc, IList<TVal> sourceList) : base(sourceList)
    {
        _keyFunc = keyFunc;

        foreach (var item in sourceList)
        {
            var key = _keyFunc(item);
            _dict.Add(key, item);
        }
    }

    public TVal FastFind(TKey key)
    {
        TVal val;
        _dict.TryGetValue(key, out val);
        return val;
    }

    protected override void InsertItem(int index, TVal val)
    {
        _dict.Add(_keyFunc(val), val);
        base.InsertItem(index, val);
    }

    protected override void SetItem(int index, TVal val)
    {
        var key = _keyFunc(val);
        _dict[key] = val;

        base.SetItem(index, val);
    }

    protected override void RemoveItem(int index)
    {
        var item = this[index];
        var key = _keyFunc(item);
        _dict.Remove(key);

        base.RemoveItem(index);
    }

    protected override void ClearItems()
    {
        _dict.Clear();
        base.ClearItems();
    }
}

:

public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    private void simpleButton1_Click(object sender, EventArgs e)
    {
        var keyedBindingList = new FastLookupBindingList<int, Person>(p => p.Id)
                                   {
                                       new Person {Id = 1, Name = "Joe"}, 
                                       new Person {Id = 2, Name = "Josephine"}
                                   };
        var person = keyedBindingList.FastFind(2);
        var unkonwn = keyedBindingList.FastFind(4);
    }
+5

All Articles