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

C# 优化在集合中添加/删除/更新新项的方式,c#,wpf,mvvm,C#,Wpf,Mvvm,我想问我是否仍然可以优化我的代码,因为目前,我就是这样写的 这是通过使用MVVM、ObservableCollection和C实现的 这是伪代码 // ClientCollection is an ObservableCollection<T> object // ClientLists is a List<T> object that came from the JSON data // // check our clients in ClientCollection

我想问我是否仍然可以优化我的代码,因为目前,我就是这样写的

这是通过使用MVVM、ObservableCollection和C实现的

这是伪代码

// 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
            // .
            // .
            // .
}
我之所以这样做,是因为使用.Clear似乎不是更新集合的好方法,首先它刷新整个列表,当您有数千个项目时,它看起来会闪烁。。这就是我为什么这么做的原因


非常感谢

有很多东西可以提高性能,但这是一个很好的起点,适合您的问题。继承自ObservaleCollection并提供进行批量更改的方法,但只引发单个事件,从而减少绑定更新:

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));
    }
}

你能发布工作代码而不是伪代码吗?有时编写代码可以帮助您找到答案。RemoveRange函数的性能将非常糟糕,即在^2上。这可以通过以下代码行轻松演示:new BulkObservableCollectionEnumerable.Range0,1000000.RemoveAngeEnumerable.Range0,1000000;更好的算法是使用哈希表,例如var map=items.ToLookupi=>i;重置this.Items.Wherei=>!map.Containsi;。这将导致内存和性能都出现问题。请看一下BulkObservableCollection类型的microsoft实现: