c# - 循环查询匹配并删除子集项
问题描述
我有以下两个表格,其中包含有关已完成项目的信息,我需要以这种方式进行报告以进行报告。
qry = db.AssemblyListItems
.AsNoTracking()
.Where(x => x.ProductionPlanID == (long)_currentPlan.ProductionPlan )
.ToList();
var _query = qry.Where(w => w.ItemCode == "EPR15CT.L01" && w.DocumentNo == "0000026590")
.SingleOrDefault();
var hasbeenAssembled = dbCompletedPrinteds
.AsNoTracking()
.Where(x => x.ProductionPlanId == (long)_currentPlan.ProductionPlan)
.ToList();
foreach (var item in hasbeenAssembled) {
qry.RemoveAll(X => X.SOPOrderReturnID == Int32.Parse(item.SopLineItemId) );
}
如果它在第二个表中找到任何匹配项,则将其从主查询中删除。
您将看到这些表中存储了许多相同的数据。但是由于某种原因,这些项目仍然显示在我需要某种方式来循环第一个查询和第二个查询并从 qry 对象中删除匹配的项目。
所以我需要做的步骤是:
循环完成并打印的对象删除具有相同文档编号和项目代码的任何匹配产品并匹配产品计划 ID 项目,然后将其从主 AssemblyListItems 查询中删除,然后在将项目保留在列表中的最小值时显示在 gui 中。
编辑 2
这会起作用,但我认为它不是很有效。
List<AssemblyListItems> _query = qry.ToList();
foreach (AssemblyListItems item in _query)
{
var hasbeenAssembled = 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)
{
qry.RemoveAll(x => x.ProductionPlanID == subitem.ProductionPlanId && x.DocumentNo == item.DocumentNo && x.ItemCode == subitem.StockCode);
}
}
}
编辑 3 显示 edmx 中的项目
解决方案
可能是这样的?
//first get list of assembled/completed items with the _currentplan's ID:
var hasbeenAssembled =
dbCompletedPrinteds
.AsNoTracking()
.Where(x => x.ProductionPlanId == (long)_currentPlan.ProductionPlan)
//note: not sure of underlying DB technology here, but this .ToList() will
//typically cause a DB query to execute here.
.ToList();
//next, use that to filter the main query.
qry = db.AssemblyListItems
.AsNoTracking()
.Where(x =>
//Get current plan items
(x.ProductionPlanID == (long)_currentPlan.ProductionPlan)
//filter out items which are in the previous list of 'completed' ones
&& (!hasBeenAssembled.Any(hba => hba.SopLineItemId==x.SOPOrderReturnID))
)
.ToList();
//I don't have any idea what _query is for, it doesn't seem to be used for anything
//in this example...
var _query = qry.Where(w => w.ItemCode == "EPR15CT.L01" && w.DocumentNo == "0000026590")
.SingleOrDefault();
推荐阅读
- automated-tests - 空手道 - 如何在画布上模拟手写签名
- r - 在另一个函数中执行一个函数的标准
- excel - 如何合并 2 个数据透视表(使用不同的过滤器)来构建数据透视图
- apache-flink - Flink 事件关联和回溯
- javascript - Ionic 5:在浏览器构建的应用程序上,如何防止重新加载时出现 404 错误
- c - 尝试使用 Wincrypt RC4 时不支持请求
- sql-server - 如何重命名 SQL Server 查询中的列
- xml - 如何使用 XSLT 搜索 XML 树节点以获取每个节点的父节点
- javascript - 请求下载具有 javascript 点击下载的文件?
- angular - Angular 项目的 l10n 和 i18n 的最佳实践是什么?