Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an empty list of a collection?

I have a collection of anonymous class and I want to return an empty list of it.

What is the best readable expression to use?

I though of the following but I don't think they are readably enough:

var result = MyCollection.Take(0).ToList();

var result = MyCollection.Where(p => false).ToList();

Note: I don't want to empty the collection itself.

Any suggestion!

like image 867
Homam Avatar asked Apr 19 '11 10:04

Homam


1 Answers

Whats about:

Enumerable.Empty<T>();

This returns an empty enumerable which is of type T. If you really want a List so you are free to do this:

Enumerable.Empty<T>().ToList<T>();
like image 90
Martin Kirsch Avatar answered Sep 21 '22 13:09

Martin Kirsch