Is there a way to remove all items except first one from any type of collection (Control.Items, List ....) using LINQ only ?
-
First one unique then others? – Emaad Ali Nov 21 '11 at 13:36
-
6LINQ is not really designed to remove items from lists. It's a query language, therefore designed to select items from lists. Deleting is then done outside LINQ. – Jens Nov 21 '11 at 13:36
-
`.Take(1)`, although that doesn't remove it from the original. – George Duckett Nov 21 '11 at 13:36
-
@Emaad Ali, every item is unique – eugeneK Nov 21 '11 at 13:38
-
@George Duckett, i can use Skip(1) as well but is there a way to remove the rest with the same call? – eugeneK Nov 21 '11 at 13:39
6 Answers
No. LINQ is designed for querying collections (no side-effects), not for adding or removing items.
What you can do is write a query that takes the first element of the collection:
var result = source.Take(1);
Note that LINQ doesn't work with all types of collections; you need a LINQ provider to make LINQ work. For instance, source
must implement IEnumerable<T> to use the extension methods of the Enumerable Class (LINQ-to-Objects).

- 213,145
- 36
- 401
- 431
How about something using reflection?
static void RemoveButFirst(object o){
Type t = o.GetType();
System.Reflection.MethodInfo rm = t.GetMethod("RemoveAt",
new Type[]{typeof(int)});
System.Reflection.PropertyInfo count = t.GetProperty("Count");
for (int n = (int)(count.GetValue(o,null)) ; n>1; n--)
rm.Invoke(o, new object[]{n-1});
}
This would work any time your collection exposed an int Count
property and a RemoveAt(int)
method, which I think those collections should.
And a more concise version, using dynamic
, if you work with C# 4.0:
public static void RemoveBut(dynamic col, int k){
for (int n = col.Count; n>k; n--)
col.RemoveAt(n-1);
}

- 18,195
- 4
- 41
- 71
-
-
thanks for creative solution using reflection but i will probably stick to simple loop... – eugeneK Nov 21 '11 at 14:03
-
If only need to do it once in your code, then a simple loop will be more effective. This solution is for the frustrating case where you need to do it more than once. – Vlad Nov 21 '11 at 14:07
You can use .Take(1)
, but it returns a new collection, and leaves the original intact.
The idea of LINQ came from functional programming where everything is immutable, because of that, they didn't make it possible to modify the collections with LINQ.
Jon Skeet has a comment on the subject: LINQ equivalent of foreach for IEnumerable<T>

- 1
- 1

- 2,240
- 2
- 16
- 19
How about (in linq):
var result = list.Where(l => l != list.First());
But this would be better:
var result = list.Take(1);

- 9,831
- 7
- 47
- 78
List<string> collection = new List<string>();
collection.RemoveAll(p => p.StartsWith("something"));

- 4,236
- 1
- 24
- 33
-
So the only way to remove is using some value of object p ? There is no way to remove using indexes ? – eugeneK Nov 21 '11 at 13:41
-
1It's worth noting `RemoveAll` doesn't exist for 'all collection types', also, this doesn't use LINQ. – George Duckett Nov 21 '11 at 13:42
-
@eugeneK You can use the removeAt() but it can only remove 1 item. – shenhengbin Nov 21 '11 at 13:43
-
@George Duckett yes , it is not use linq. but linq is just for query . and for the List
has RemoveAll method. – shenhengbin Nov 21 '11 at 13:46 -
I know, but you might want to point that out in the answer as dtb as done in his. – George Duckett Nov 21 '11 at 13:54
listXpto.Where(x=>true /* here goes your query */)
.Select(x=>{listXpto.Remove(x); return null})
But I don´t know the real utility of that.
Remember that the remove method is for ILists, not IQueryable in general.

- 38,117
- 9
- 79
- 111

- 2,761
- 21
- 37