[英]Remove parents from list
I have 2 lists, both containing objects which look like this: (this is a simplification) 我有2个列表,都包含看起来像这样的对象:(这是一个简化)
class ddItem
{
public string Code;
public string Key;
public string ParentKey;
}
One list contains items which may or may not have children in the other list. 一个列表包含在另一个列表中可能有或没有孩子的项目。 I'm trying to figure out a nice way to remove items from the parent list if they have a corresponding item in the child list, ie where parent.Key = child.parentKey.
我试图找出一种从父级列表中删除项目的好方法,如果它们在子级列表中具有相应的项目,即parent.Key = child.parentKey。
This is the LINQ I have, and it's currently causing me to lose brain cells: 这是我拥有的LINQ,目前正在导致我失去脑细胞:
parentList =
(List<ddItem>)parentList.Where(p => childList.Select(c => c.ParentKey == p.Key));
Currently I have a red squiggly line under childList.Select(c => c.ParentKey == p.Key)
and the message Cannot convert expression type 'System.Collections.Generic.IEnumerable<bool>' to return type 'bool'
so I must be missing a cast somewhere - I think... 目前我在
childList.Select(c => c.ParentKey == p.Key)
下有一条红色的波浪线,并且消息Cannot convert expression type 'System.Collections.Generic.IEnumerable<bool>' to return type 'bool'
因此我一定在某个地方想念演员-我想...
[EDIT] [编辑]
For posterity, the correct code is: 对于后代,正确的代码是:
parentList =
parentList.Where(p => childList.Any(c => c.ParentKey == p.Key)).ToList();
(I also had to move the cast) (我也不得不移动演员)
[/EDIT] [/编辑]
我认为您只需要从select
更改为any
where
parentList.Where(p => childtList.Any(c => c.ParentKey == p.Key))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.