繁体   English   中英

从列表中删除父母

[英]Remove parents from list

我有2个列表,都包含看起来像这样的对象:(这是一个简化)

class ddItem
{
    public string Code;
    public string Key;
    public string ParentKey;
}

一个列表包含在另一个列表中可能有或没有孩子的项目。 我试图找出一种从父级列表中删除项目的好方法,如果它们在子级列表中具有相应的项目,即parent.Key = child.parentKey。

这是我拥有的LINQ,目前正在导致我失去脑细胞:

parentList = 
    (List<ddItem>)parentList.Where(p => childList.Select(c => c.ParentKey == p.Key));

目前我在childList.Select(c => c.ParentKey == p.Key)下有一条红色的波浪线,并且消息Cannot convert expression type 'System.Collections.Generic.IEnumerable<bool>' to return type 'bool'因此我一定在某个地方想念演员-我想...

[编辑]

对于后代,正确的代码是:

parentList = 
    parentList.Where(p => childList.Any(c => c.ParentKey == p.Key)).ToList();

(我也不得不移动演员)

[/编辑]

我认为您只需要从select更改为any where

parentList.Where(p => childtList.Any(c => c.ParentKey == p.Key))

暂无
暂无

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

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