c# - 从列表中删除一系列项目而不循环
问题描述
嗨,有没有更优雅的方式来做这件事我必须做循环是否像范围功能一样我可以删除所有找到的项目
抱歉,我应该展示我的 qry 是如何插入的。
顺便说一句,这是我要从中删除的两个不同实体,希望您能理解。
var qry = db.AssemblyListItems.AsNoTracking().Where(x =>
x.ProductionPlanID == (long)_currentPlan.ProductionPlan ).ToList();
var hasbeenAssembled = db.CompletedPrinteds.AsNoTracking().Where(x =>
x.ProductionPlanId == item.ProductionPlanID).ToList();
var hasbeenFound = db.CompletedPrinteds.AsNoTracking().Where(x =>
x.ProductionPlanId== item.ProductionPlanID).ToList();
foreach (var subitem in hasbeenAssembled )
{
if(item.ProductionPlanID ==subitem.ProductionPlanId && item.DocumentNo == subitem.DocumentNo && item.DocumentNo == subitem.DocumentNo && item.OutstandingToMake ==0)
{
qry.RemoveAll(x => x.ProductionPlanID == subitem.ProductionPlanId && x.DocumentNo == item.DocumentNo && x.ItemCode == subitem.StockCode && item.OutstandingToMake ==0);
}
}
public List<AssemblyListItems> RemoveDespatchedItems(List<AssemblyListItems> AssemblyItems)
{
foreach (AssemblyListItems item in AssemblyItems)
{
using (var db = new LiveEntities())
{
var hasNotBeenDespatched = db.PalletizedItems.Where(w => w.Despatched != "Not Despatched");
foreach (var subitem in hasNotBeenDespatched)
{
AssemblyItems.RemoveAll(x => x.ProductionPlanID == subitem.ProductionPlanID && x.DocumentNo == item.DocumentNo && x.ItemCode == subitem.StockCode);
}
}
}
return AssemblyItems;
}
我只需要从第二个查询中删除第一个查询 hasNotBeenDespatched 中的项目。可能有超过 400 个项目,我希望它尽可能高效。
编辑 2 我离我们更近了一点,谢谢,但它仍然没有从 assebmittems 中删除的已删除espatchitems 中删除项目我不知道为什么
public List<AssemblyListItems> RemoveDespatchedItems(List<AssemblyListItems> AssemblyItems, Int64 ProductionPlanId)
{
using (var db = newLiveEntities())
{
List<PalletizedItems> removeDespatchItems = db.PalletizedItems.Where(w => w.Despatched != "Not Despatched" && w.ProductionPlanID == ProductionPlanId).ToList();
var itemsDocumentNo = db.PalletizedItems.Select(x => x.ProductionPlanItemID).ToList();
foreach (var subitem in removeDespatchItems) {
AssemblyItems.RemoveAll(x => x.ProductionPlanID == subitem.ProductionPlanID && itemsDocumentNo.Contains(x.ProductionPlanItemID) && x.ItemCode == subitem.StockCode && x.LineQuantity==x.AllocatedQuantity);
}
}
return AssemblyItems;
}
解决方案
不是 100% 我完全明白它应该是怎样的。
但是,一般来说,您可以使用join
这将导致它在数据库中完成。像这样的东西:
var remainingItems = (from ali in db.FUEL_AssemblyListItems
join completed in db.FuelCompletedPrinteds
on new { ali.ProductionPlanID, ali.DocumentNo, ali.ItemCode } equals new { completed.ProductionPlanID, completed.DocumentNo, completed.StockCode }
join dispatched in db.FUEL_PalletizedItems
on new { ali.ProductionPlanID, ali.DocumentNo, ali.ItemCode } equals new { dispatched.ProductionPlanID, dispatched.DocumentNo, dispatched.StockCode }
where (ali.ProductionPlanID == (long) _currentPlan.ProductionPlan
&& ali.DocumentNo == completed.DocumentNo
&& completed.OutstandingToMake == 0
&& dispatched.Despatched != "Not Despatched")
select ali).ToList();
根据数据库中的记录,连接可能需要是一个外部连接,它需要稍微不同的语法,但希望您有一个起点。
推荐阅读
- .net-core - Microsoft.ML 从 .netcore 移植到 .net 框架 4.7.2
- python - 如何覆盖 Django 中的 handler404 和 handler505 错误?
- psychopy - ValueError:在 TextStim 中设置 pos 时操作不支持的值“0.5”
- react-native - 具有锐利边缘的 React Native 样式视图
- android - Android BLE 扫描在后台某个时间后停止
- php - 除了用户在 Laravel Passport 中注册/登录之外,如何使用不同的模型进行身份验证?
- python - 鹡鸰分页,django,鹡鸰,python
- javascript - 如何有效地将一个对象拆分为数组内的多个对象
- html - 最初在页面加载时出现的单选按钮波纹效果
- linux - 使用 shell 脚本自动输入