如何删除使用linq匹配条件的所有列表行?

时间:2016-08-24 10:30:24

标签: c# linq where-clause removeall

我正在尝试删除匹配where条件的List<T>中的所有记录。我在linq中找到的是RemoveAll()方法,但它似乎只能通过删除匹配条件的属性而不是列表中的complete row来实现。

所以我尝试将all全部删除为suggested here并结合使用导致"argument null exception".

的Where子句

问题:

如何使用linq删除所有符合条件的列表行?

//Sort the list by the UpdatedTime time descending
//Remove all records in list that are older than today's date and status equal to BB. Then order the remaining records desc.
var cuttOff = DateTime.UtcNow.AddDays(-10);
List<Escalation> escHistorySorted = escHistory.RemoveAll.Where(x => x.UpdatedTime  <= cuttOff && x.status == "BB").OrderByDescending(d => d.UpdatedTime).ToList();

1 个答案:

答案 0 :(得分:2)

看起来你一下子想要做太多。

首先删除记录(直接从azure account import /path/to/.publishsettings_file 内的where子句移动谓词),然后对它们进行排序。

RemoveAll

var cuttOff = DateTime.UtcNow.AddDays(-10); escHistory.RemoveAll(x => x.UpdatedTime <= cuttOff && x.status == "BB"); List<Escalation> escHistorySorted = escHistory.OrderByDescending(d => d.UpdatedTime).ToList(); 的返回值是一个整数,表示已删除的记录数,因此您无法简单地对调用该方法的结果进行排序。