简体   繁体   中英

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

If you want to destructively remove the offending item from the list then use the RemoveAll(Predicate<T>) method; 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 :

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.

You can use Where like...

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:

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. 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 .

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:

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

There are two extensions you could care about:

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 :

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);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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