我正在尝试删除匹配where条件的List<T>
中的所有记录。我在linq中找到的是RemoveAll()
方法,但它似乎只能通过删除匹配条件的属性而不是列表中的complete row
来实现。
所以我尝试将all全部删除为suggested here并结合使用导致"argument null exception".
问题:
如何使用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();
答案 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();
的返回值是一个整数,表示已删除的记录数,因此您无法简单地对调用该方法的结果进行排序。