简体   繁体   English

从集合中删除/跳过项目的最佳方法是什么

[英]What is the best way to remove/skip an item from collection

what is the best way to remove/skip an item from collection object 从集合对象中删除/跳过项目的最佳方法是什么

List<Person> personList = new List<Person>()
personList  = dao.GetData(123);
personList = personList.Select(x => x.Comment... ????

resultset: 结果集:

"GCE"   
Not available 
""               //comments
"RES" 
9.97000000 
9.99000000 

........
........
........

so, i am targeting the field called "comments" and if the comments are empty then dont render. 所以,我的目标是“评论”字段,如果评论是空的,那么不要渲染。

i could have do that in foreach loop with an if condition but i am looking for the best practice 我可以在foreach循环中使用if condition但我正在寻找最佳实践

If you want to destructively remove the offending item from the list then use the RemoveAll(Predicate<T>) method; 如果要破坏性地从列表中删除违规项,请使用RemoveAll(Predicate<T>)方法; it removes every item that matches a predicate from the list: 它会从列表中删除与谓词匹配的所有项:

myList.RemoveAll(x=>x.Comment == whatever);

If you want to keep the list the same and make a filtered sequence of items then use Where : 如果要保持列表相同并生成过滤的项目序列 ,请使用Where

foreach(Item item in myList.Where(x=>x.Comment != whatever))
    ...

That keeps the list the same; 这使列表保持不变; the Where just gives you a "view" of the list that has the filter applied to it. Where只是为您提供了应用过滤器的列表的“视图”。

You can use Where like... 你可以使用Where ...

foreach (var a in personList.where(x => !string.IsNullOrWhitespace(x.Comment))
{
   // code
}

This will restrict the list to comments that have a non-null and non-whitespace string: 这会将列表限制为具有非null和非空白字符串的注释:

List<Person> personList = dao.GetData(123); 
filteredList = personList.Where(x => !String.IsNullOrWhitespace(x.Comment));

You can use LINQ to filter the collection before you even use it. 您甚至可以在使用之前使用LINQ过滤集合。 For starters, this is kind of redundant: 对于初学者来说,这有点多余:

List<Person> personList = new List<Person>()
personList  = dao.GetData(123);
personList = personList.Select(x => x.Comment... ????

It sounds like what you're looking for is this: 听起来你正在寻找的是这样的:

var personList = dao.GetData(123)
                    .Where(p => !string.IsNullOrWhitespace(p.Comment))
                    .Select(p => ...

Then you can loop through the items in personList . 然后你可以遍历personList的项目。

Of course, looking back, if you don't even need the .Select() (that is, if it's not doing anything other than an attempt to filter), then this is simpler: 当然,回过头来看,如果你甚至不需要 .Select() (也就是说,除了尝试过滤之外没有做任何事情),那么这更简单:

var personList = dao.GetData(123)
                    .Where(p => !string.IsNullOrWhitespace(p.Comment))

There are two extensions you could care about: 您可以关注两个扩展:

Where : Where

var subset = collection.Where(x => x != someValue);

This method simply takes all the elements and applies the predicate to it, yielding only the matching elements. 此方法只接受所有元素并将谓词应用于它,仅产生匹配元素。

And Skip , SkipWhile : SkipSkipWhile

var skipFirstThreeItems = collection.Skip(3);
var skippedItems = collection.SkipWhile(x => x != "SomeValue")

The important distinction with the second one is that it will skip values until the predicate is matched, and then it will take all subsequent elements. 与第二个区别的重要区别是它将跳过值直到谓词匹配,然后它将采用所有后续元素。

I often think that the best way is debatable, but I like to be explicit about what the code is doing: 我经常认为最好的方法是有争议的,但我喜欢明确代码的作用:

var peopleWithComments = dao.GetData(123)
  .Where(person => person.Comment.IsPresent());

...

public static class StringExtensions {
  public bool IsPresent(this string self) {
    return !String.IsNullOrWhitespace(self);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM