我还可以使用哪些LINQ运算符/方法来优化基于C#Collections的代码?

时间:2021-09-07 20:03:08

Is there a way (using LINQ possibly) I can optimize the below code. What this code does is adds, removes and updates the items in the variable myFirstCollection by comparing the items in variable mySecondCollection. Can somehow I use more LINQ methods/queries or other LINQ operators so that the myFirstCollection variable is compared and updated as quickly as possible.

有没有办法(可能使用LINQ)我可以优化下面的代码。这段代码的作用是通过比较变量mySecondCollection中的项来添加,删除和更新变量myFirstCollection中的项。可以以某种方式我使用更多的LINQ方法/查询或其他LINQ运算符,以便尽快比较和更新myFirstCollection变量。

To add to my requirement, my ObservableCollection variable myfirstCollection is bound to the WPF Grid so that the additions, deletions and, updates are reflected in grid immediately with data binding.

为了增加我的要求,我的ObservableCollection变量myfirstCollection被绑定到WPF网格,以便添加,删除和更新立即反映在网格中,并带有数据绑定。

private void UpdateFirstCollection(ObservableCollection<MyDTO> myFirstCollection, IEnumerable<MyDTO> mySecondCollection)
{
//Code to add items to First Collection, how to optimize this block?
    foreach (var item in mySecondCollection.ToList())
    {
        if (!myFirstCollection.Contains(item))
        {
            myFirstCollection.Add(item);
        }
        else
        {
//Code to update items from First Collection, how to optimize this block?
            var itemToUpdate = myFirstCollection.FirstOrDefault(dto => dto.ID == item.ID);
            if (itemToUpdate != null)
            {
                itemToUpdate = item;
            }
        }
    }

//Code to remove items from First Collection, how to optimize this block?
    var copy = new ObservableCollection<myDTO>(myFirstCollection);
    foreach (var item in copy.ToList())
    {
        if (!mySecondCollection.Contains(item))
        {
            myFirstCollection.Remove(item);
        }
    }
}

Any help to Refactor / Improve the above code based on any articles/links is highly appreciated.

任何有关基于任何文章/链接重构/改进上述代码的帮助都非常感谢。

2 个解决方案

#1


0  

Maybe I'm missing something, but since you're only adding things to myFirstCollection that exist in mySecondCollection and then removing things that don't exist in mySecondCollection, why don't you just clear myFirstCollection and add everything from mySecondCollection?

也许我错过了一些东西,但是因为你只是在mySecondCollection中添加了存在于mySecondCollection中的myFirstCollection,然后删除了mySecondCollection中不存在的东西,你为什么不清除myFirstCollection并添加mySecondCollection中的所有内容?

private void UpdateFirstCollection(ObservableCollection<MyDTO> myFirstCollection, IEnumerable<MyDTO> mySecondCollection)
{
    myFirstCollection.Clear();
    foreach(var item in mySecondCollection)
    {
        myFirstCollection.add(item);
    }
}

Please tell me if I've over-simplified this.

请告诉我,我是否过度简化了这一点。

#2


0  

You're adding any new items, removing any removed items, and updating any changed items. You are essentially replacing the entire collection.

您正在添加任何新项目,删除任何已删除的项目,以及更新任何已更改的项目。您基本上正在替换整个集合。

myFirstCollection = mySecondCollection;

I know this might not be desirable from an ObservableCollection point of view, so instead

我知道从ObservableCollection的角度来看这可能是不可取的,所以相反

myFirstCollection.Clear();
foreach (var item in mySecondCollection)
    myFirstCollection.Add(item);

#1


0  

Maybe I'm missing something, but since you're only adding things to myFirstCollection that exist in mySecondCollection and then removing things that don't exist in mySecondCollection, why don't you just clear myFirstCollection and add everything from mySecondCollection?

也许我错过了一些东西,但是因为你只是在mySecondCollection中添加了存在于mySecondCollection中的myFirstCollection,然后删除了mySecondCollection中不存在的东西,你为什么不清除myFirstCollection并添加mySecondCollection中的所有内容?

private void UpdateFirstCollection(ObservableCollection<MyDTO> myFirstCollection, IEnumerable<MyDTO> mySecondCollection)
{
    myFirstCollection.Clear();
    foreach(var item in mySecondCollection)
    {
        myFirstCollection.add(item);
    }
}

Please tell me if I've over-simplified this.

请告诉我,我是否过度简化了这一点。

#2


0  

You're adding any new items, removing any removed items, and updating any changed items. You are essentially replacing the entire collection.

您正在添加任何新项目,删除任何已删除的项目,以及更新任何已更改的项目。您基本上正在替换整个集合。

myFirstCollection = mySecondCollection;

I know this might not be desirable from an ObservableCollection point of view, so instead

我知道从ObservableCollection的角度来看这可能是不可取的,所以相反

myFirstCollection.Clear();
foreach (var item in mySecondCollection)
    myFirstCollection.Add(item);