Select All that do not already exist in destination

£可爱£侵袭症+ 提交于 2020-01-06 03:37:26

问题


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.


回答1:


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.




回答2:


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

myArray.Where(o => !otherArray.Any(inner => inner.id == o.id));


来源:https://stackoverflow.com/questions/8657760/select-all-that-do-not-already-exist-in-destination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!