Jon*_*ood 2 c# linq linq-to-entities entity-framework
我试图使用Entity Framework删除一些项目.我想删除那些DestinationId不在的项目int[] destinationIds.(我正在删除联结表中的行.)
foreach (var destination in product.ImportedProductDestinations.Where(ipd => !destinationIds.Contains(ipd.DestinationId)))
product.ImportedProductDestinations.Remove(destination);
Run Code Online (Sandbox Code Playgroud)
但该foreach声明给出了运行时错误:
收集被修改; 枚举操作可能无法执行.
好吧,我想我理解这个错误.但我怎么能做我想做的事呢?
小智 5
不要从正在扫描的集合中删除该项目,将其从db上下文中删除
context.IMportedProductDestinations.DeleteObject(destination);
Run Code Online (Sandbox Code Playgroud)
然后使用context.SaveChanges将更改应用于db
| 归档时间: |
|
| 查看次数: |
992 次 |
| 最近记录: |