循环查询匹配并删除子集项

时间:2018-10-23 11:35:57

标签: c# linq

我有以下两个表格,其中包含有关完成的项目的信息,出于报告目的,我需要这样做。

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对象中删除匹配的项目。

enter image description here

所以我需要做的步骤是:

已完成并打印的对象删除具有相同文档编号和项目代码的所有匹配产品,并匹配productplan id项目,然后将其从主AssemblyListItems查询中删除,然后在最小化gui中分发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中显示项目

enter image description here

2 个答案:

答案 0 :(得分:0)

上周我在下面使用“左外部联接”进行查询,以获取三组数据

var results = (from srs in srsEmps
               join dest in destEmps on srs.EmpCode equals dest.EmpCode into dsNull
               from dest in dsNull.DefaultIfEmpty()
               select new { srs = srs, dest = dest }).ToList();

var Common = results.Where(x => (x.srs != null) && ( x.dest != null)).ToList();
var Deleted = results.Where(x => x.dest != null).ToList();
var NewlyAdded = results.Where(x => x.srs != null);

答案 1 :(得分:0)

也许是这样?

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