简体   繁体   English

优化添加/删除/更新集合中新项目的方式

[英]Optimize way of adding/removing/updating a new item in a collection

I want to ask if I could still optimized my code, because currently, this is how I wrote it. 我想问一下我是否仍然可以优化代码,因为目前,这就是我编写代码的方式。

This is by the way using MVVM, ObservableCollection, and C# 这是通过使用MVVM,ObservableCollection和C#进行的

here's the pseudocode 这是伪代码

// ClientCollection is an ObservableCollection<T> object
// ClientLists is a List<T> object that came from the JSON data
//
// check our clients in ClientCollection
// if a client was removed in List<T>, then delete this client
// in our ClientCollection.
ToRemoveLists toremove;
{ 
    for each Clients client in ClientCollection
        var a = from b in ClientLists
                where b.name == client.name
                select b;

        if a.count() == 0 
            toremove.Add a;
}
{ // remove client from ClientCollection
    for each Clients client in toremove
        ClientCollection.Remove client;
}

// now add new clients if there are any
// or update client info if this client exists
{ 
    for each Clients client in ClientLists
        var a = from b in ClientCollection
                where b.name == client.name
                select b;

        if a.count() == 0 
            ClientCollection.Add clients
        else
            // update client info
            // .
            // .
            // .
}

The reason why I did this is because using .Clear() looks like not a good way of updating the collection, first it refreshes the entire lists, it looked like it was flickering when you have thousands of items.. so that's why I done. 之所以这样做,是因为使用.Clear()似乎不是更新集合的好方法,首先它刷新整个列表,当您有成千上万个项目时,它似乎在闪烁。.所以这就是我这样做的原因。

THanks a bunch 表示一堆

There are many things to help performance, but this is a good place to start and fits your question. 有很多事情可以提高性能,但这是一个很好的起点,可以解决您的问题。 Inherit from ObservableCollection and provide methods to do bulk changes, but only raise a single event, thus reducing binding updates: 继承自ObservableCollection并提供进行批量更改的方法,但仅引发单个事件,从而减少绑定更新:

public class BulkObservableCollection<T> : ObservableCollection<T>
{
    public BulkObservableCollection()
    {
    }

    public BulkObservableCollection(List<T> list) : base(list)
    {
    }

    public BulkObservableCollection(IEnumerable<T> collection) : base(collection)
    {
    }

    /// <summary>
    /// Adds a range if items to the collection, causing a reset event to be fired.
    /// </summary>
    /// <param name="items"></param>
    public void AddRange(IEnumerable<T> items)
    {
        if (items == null) throw new ArgumentNullException("items");

        var intialCount = Items.Count;
        Items.AddRange(items);            
        if (Items.Count != intialCount)
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    /// <summary>
    /// Removes a range of items from the collection, causing a reset event to be fired.
    /// </summary>
    /// <param name="items"></param>
    public void RemoveRange(IEnumerable<T> items)
    {
        if (items == null) throw new ArgumentNullException("items");

        var intialCount = Items.Count;
        foreach (var item in items)            
            Items.Remove(item);
        if (Items.Count != intialCount)
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    public void Reset(IEnumerable<T> newItems)
    {
        if (newItems == null) throw new ArgumentNullException("newItems");

        Items.Clear();
        Items.AddRange(newItems);

        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM