What is the preferable way for transferring some items (not all) from one list to another.
What I am doing is the following:
var selected = from item in items where item.something > 10 select item; otherList.AddRange(selected); items.RemoveAll(item => selected.Contains(item));
In the interest of having the fastest/best code there is, is there a better way?
Use the AddRange() method to append a second list to an existing list. list1. AddRange(list2);
Method : Using pop() + insert() + index() This particular task can be performed using combination of above functions. In this we just use the property of pop function to return and remove element and insert it to the specific position of other list using index function.
Try this: Int32 length = yourList. Count; In C#, arrays have a Length property, anything implementing IList<T> (including List<T> ) will have a Count property.
I'd try @Mehrdad's answer, and maybe test it against this one too...
var selected = items.Where(item => item.Something > 10).ToList(); selected.ForEach(item => items.Remove(item)); otherList.AddRange(selected);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With