Does C# offer some nice method to cast a single entity of type T
to IEnumerable
?
The only way I can think of is something like:
Your call to AsEnumerable()
is unnecessary. AsEnumerable
is usually used in cases where the target object implements IQueryable
but you want to force it to use LINQ-to-Objects (when doing client-side filtering on a LINQ-compatible ORM, for example). Since List
implements IEnumerable
but not IQueryable
, there's no need for it.
Anyway, you could also create a single-element array with your item;
IEnumerable enumerable = new[] { t };
Or Enumerable.Repeat
IEnumerable enumerable = Enumerable.Repeat(t, 1);