Select All that do not already exist in destinatio

2019-08-15 06:43发布

I have a simple set of arrays, kind of like this .. structure shortened for brevity and such. Basically it is a list of identities with an additional field (a sort of dictionary).

[
 { 
   "Id" : 1, 
   "Requirement" : { 
     "Value" : "2", "Name" : "Orders" 
    }
 },
 { 
   "Id" : 2, 
   "Requirement" : { 
     "Value" : "4", "Name" : "Orders" 
    }
 },
 { 
   "Id" : 3, 
   "Requirement" : { 
     "Value" : "6", "Name" : "Orders" 
    }
 },
 { 
   "Id" : 4, 
   "Requirement" : { 
     "Value" : "8", "Name" : "Orders" 
    }
 },
]

I need to be constantly checking another value against this array, and pulling in the items that are satisfied (for instance, the 'user' has an arbitrary value of Orders that is an integer. Each time Orders updates, I want to get all items out of the array where Orders is greater than or equal to the Requirement value, but without pulling in values they already have)

So then, this would work as follows ... User has 1 Order. (Nothing Happens) User has 2 Order. (Id 1 is pulled when the user's update operation runs) User then achieves 4 Order. (Id 2 is pulled in, but Id 1 already exists, so it is skipped)

Is there a simplistic way to achieve this with a LINQ query? I don't have the luxury of storing the 'last checked' value. The data structure is not one I can modify at this point in time.

2条回答
女痞
2楼-- · 2019-08-15 07:01
Orders.Where(o => o.Requirement.Value > requirment).Except(processedOrders);

First select the Orders that meet the requirement, then using the Except remove the orders that have already been processed.

查看更多
闹够了就滚
3楼-- · 2019-08-15 07:14

You can use the Any method to check for values not in another array:

myArray.Where(o => !otherArray.Any(inner => inner.id == o.id));
查看更多
登录 后发表回答