从集合对象中删除/跳过项目的最佳方法是什么
List<Person> personList = new List<Person>()
personList = dao.GetData(123);
personList = personList.Select(x => x.Comment... ????
Run Code Online (Sandbox Code Playgroud)
结果集:
"GCE"
Not available
"" //comments
"RES"
9.97000000
9.99000000
........
........
........
Run Code Online (Sandbox Code Playgroud)
所以,我的目标是"评论"字段,如果评论是空的,那么不要渲染.
我可以在foreach循环中做到这一点,if condition但我正在寻找最好的做法
如果您想从列表中破坏性地删除违规项目,请使用该RemoveAll(Predicate<T>)方法; 它会从列表中删除与谓词匹配的所有项:
myList.RemoveAll(x=>x.Comment == whatever);
Run Code Online (Sandbox Code Playgroud)
如果要保持列表相同并生成过滤的项目序列,请使用Where:
foreach(Item item in myList.Where(x=>x.Comment != whatever))
...
Run Code Online (Sandbox Code Playgroud)
这使列表保持不变; 的Where只是给你一个有应用了过滤器列表的"视图".
你可以用Where...
foreach (var a in personList.where(x => !string.IsNullOrWhitespace(x.Comment))
{
// code
}
Run Code Online (Sandbox Code Playgroud)