假设我有LINQ查询,例如:
var authors = from x in authorsList
where x.firstname == "Bob"
select x;
Run Code Online (Sandbox Code Playgroud)
鉴于它authorsList是类型List<Author>,我如何删除查询返回的Author元素?authorsListauthors
或者换句话说,如何删除所有名字等于Bob的名字authorsList?
注意:这是用于问题目的的简化示例.
我正在试图弄清楚如何遍历我想要从另一个项目列表中删除的项目的通用列表.
所以我要说这是一个假设的例子
List<car> list1 = GetTheList();
List<car> list2 = GetSomeOtherList();
Run Code Online (Sandbox Code Playgroud)
我想用foreach遍历list1并删除List1中也包含在List2中的每个项目.
由于foreach不是基于索引的,我不太清楚如何去做.
如何使用LINQ从IList中删除基于另一个IList的某些元素.我需要从list1中删除记录,其中ID存在于list2中.下面是代码示例,
class DTO
{
Prop int ID,
Prop string Name
}
IList<DTO> list1;
IList<int> list2;
foreach(var i in list2)
{
var matchingRecord = list1.Where(x.ID == i).First();
list1.Remove(matchingRecord);
}
Run Code Online (Sandbox Code Playgroud)
我就是这样做的,有没有更好的方法来做同样的事情.
我正在尝试List<int>从List<int>items2中删除项目
List<int> item1 = new List<int> {1,2,3,4,5,6,7,8,9,10};
List<int> item2 = new List<int> {1,2,3,4};
Run Code Online (Sandbox Code Playgroud)
从item1中删除item2值后,所需的结果为
item1 = {5,6,7,8,9,10 }
Run Code Online (Sandbox Code Playgroud)
是否有任何直接方法或任何其他方法从另一个项目列表的内容中删除一个项目列表的内容,而不使用"for"或"foreach"?