首页 > 解决方案 > 循环查询匹配并删除子集项

问题描述

我有以下两个表格,其中包含有关已完成项目的信息,我需要以这种方式进行报告以进行报告。

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 中的项目

在此处输入图像描述

标签: c#linq

解决方案


可能是这样的?

//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();

推荐阅读