简体   繁体   English

Linq查询帮助需要

[英]Linq Query Help Needed

Say I have the following LINQ queries: 假设我有以下LINQ查询:

var source = from workflow in sourceWorkflowList
             select new { SubID = workflow.SubID,
                          ReadTime = workflow.ReadTime,
                          ProcessID = workflow.ProcessID,
                          LineID = workflow.LineID };

var target = from workflow in targetWorkflowList
             select new { SubID = workflow.SubID,
                          ReadTime = workflow.ReadTime,
                          ProcessID = workflow.ProcessID,
                          LineID = workflow.LineID };

var difference = source.Except(target);

sourceWorkflowList and targetWorkflowList have the exact same column definitions. sourceWorkflowListtargetWorkflowList具有完全相同的列定义。 But they both contain more columns of data than what is shown in the queries above. 但是它们都包含比上面查询中显示的更多的数据列。 Those are just the columns needed for this particular issue. 这些只是这个特定问题所需的列。

difference contains all rows in sourceWorkflowList that are not contained in targetWorkflowList difference包含sourceWorkflowList中未包含在targetWorkflowList中的所有行

Now what I would like to do is to remove all rows from sourceWorkflowList that do not exist in difference . 现在我想要做的是从sourceWorkflowList中删除不存在difference所有行。 Could someone show me a query that would do this? 有人能告诉我一个可以做到这一点的查询吗?

Thanks very much - Randy 非常感谢 - 兰迪

What you actually want is what's in the source and not in (what's in the source and not in target): S(S\\T) = S CUT T 你真正想要的是源中的内容而不是(源中有什么而不​​是目标):S(S \\ T)= S CUT T

var result = from sourceWorkflow in sourceWorkflowList
             join targetWorflow in targetWorkflowList on
                 new {sourceWorkflow.SubID, sourceWorkflow.ReadTime, sourceWorkflow.ProcessID, sourceWorkflow.LineID}
                 equals
                 new {targetWorflow.SubID, targetWorflow.ReadTime, targetWorflow.ProcessID, targetWorflow.LineID}
             select sourceWorkflow;

And in a different form (but this will only give you the 4 columns): 并以不同的形式(但这只会给你4列):

var result = sourceWorkflowList.Select(workflow => new {workflow.SubID, workflow.ReadTime, workflow.ProcessID, workflow.LineID})
    .Intersect(sourceWorkflowList.Select(workflow => new {workflow.SubID, workflow.ReadTime, workflow.ProcessID, workflow.LineID}));

Assuming you're using a List<T> : 假设您正在使用List<T>

sourceWorkflowList.RemoveAll(
    workflow => difference.Contains(
                    new { 
                             SubID = workflow.SubID,
                             ReadTime = workflow.ReadTime,
                             ProcessID = workflow.ProcessID,
                             LineID = workflow.LineID 
                         }));

Apologies for formatting... 抱歉格式化......

If you have a constraint that requires you to only make the change in the original container do a Remove as suggested by @rob-fonseca-ensor, 如果您有一个约束,要求您只在原始容器中进行更改,请执行@ rob-fonseca-ensor建议的删除,

If the difference list is large consider converting it to a HashSet() to get fast lookups first. 如果差异列表很大,请考虑将其转换为HashSet()以首先获得快速查找。

Otherwise... 除此以外...

If you can change the way you are getting difference use the join/intersect option suggested by @brickner as this prevents multiple iterations of the list. 如果您可以改变获得差异的方式,请使用@brickner建议的连接/交叉选项,因为这可以防止列表的多次迭代。

If a new collection is acceptable but you already have difference (cannot replace the code that generates it): 如果新集合是可接受的但您已经有差异 (无法替换生成它的代码):

var changedSource = source.Except(difference);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM