List<T>
to IEnumerable<T>
List<T>
implements IEnumerable<T>
(and many other such as IList<T>, ICollection<T>
) therefore there is no need to convert a List back to IEnumerable since it already a IEnumerable<T>
.
Example:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
Person person1 = new Person() { Id = 1, Name = "Person 1" };
Person person2 = new Person() { Id = 2, Name = "Person 2" };
Person person3 = new Person() { Id = 3, Name = "Person 3" };
List<Person> people = new List<Person>() { person1, person2, person3 };
//Converting to an IEnumerable
IEnumerable<Person> IEnumerableList = people;
You can also use Enumerable.AsEnumerable()
method
IEnumerable<Person> iPersonList = people.AsEnumerable();
IEnumerable<T>
to List<T>
IEnumerable<Person> OriginallyIEnumerable = new List<Person>() { person1, person2 };
List<Person> convertToList = OriginallyIEnumerable.ToList();
This is useful in Entity Framework.