简体   繁体   English

LINQ / Objects如何删除项目并将其添加到另一个列表

[英]LINQ / Objects howto remove items and add them to another list

I'm using Linq with objects and have a question. 我对对象使用Linq,并有一个问题。

Actual workload is processed in a background job. 实际工作量在后台作业中处理。

The actual workload should not be put to further tasks. 实际的工作量不应再用于其他任务。 It is more important, that the method which processes the actual workitems does it quite fast and gives a result, therefore the complete workload is put to chunks of workitems. 更重要的是,处理实际工作项的方法可以非常快速地完成并给出结果,因此将全部工作负荷分配给工作项块。

At a time, I only process the first n objects of the list, therefore I take n objects and then remove them from the complete list of workitems. 一次,我只处理列表中的前n个对象,因此,我将处理n个对象,然后将它们从工作项的完整列表中删除。

Roughly:

int amount = 100;
List<WorkItem> actualWorkload = null;

lock(lockWorkload){ // complete Workload is filled in other thread
    if (actualWorkload.Count > amount)
    {
        actualWorkload.AddRange(completeWorkload.Take(amount));
    }
    else
    {
        actualWorkload.AddRange(completeWorkload);
    }
    completeWorkload.RemoveAll(x => actualWorkload.Contains(x));
}
//do something with workitems Process(actualWorkload);

My question is: can 'Take' and 'Remove' be somehow combined, so that there is only one step to take items and directly remove this items from a list? 我的问题是:可以以某种方式组合“取用”和“取下”,以便仅一步就可以拿走物品并将其从列表中直接删除吗? I search fro something liek the 'Take' of a BlockingCollection which removes an item while it is returned. 我搜索类似于BlockingCollection的“ Take”,它在返回项目时将其删除。

Even if there would be an operation that allows that, in the background it would still need to remove and add them. 即使会有允许这样做的操作,在后台仍然需要删除和添加它们。

But maybe you don't need two lists? 但是也许您不需要两个列表? Maybe you could have only one list with all items, and each item has a status that you set to a different value or something. 可能只有一个包含所有项目的列表,并且每个项目的状态都设置为其他值或其他值。 But of course, I don't know your exact case so this may not be feasible. 但是,当然,我不知道您的确切情况,因此这可能不可行。

"Take and remove" sounds to me a lot like "Dequeue", so perhaps you could just use a Queue or a ConcurrentQueue ? “ Take and remove”在我看来很像“ Dequeue”,所以也许您可以只使用QueueConcurrentQueue

However, the enumerator does NOT remove items from the queue, so you would have to call Dequeue repeatedly yourself. 但是,枚举器不会从队列中删除项目,因此您必须自己反复调用Dequeue

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

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