IEnumerable<Book> _Book_IE
List<Book> _Book_List
How shall I do in order to convert _Book_List
into IEnumerable
format?
This question is related to
c#
asp.net-mvc
c#-4.0
ienumerable
As far as I know List<T>
implements IEnumerable<T>
. It means that you do not have to convert or cast anything.
You can use the extension method AsEnumerable in Assembly System.Core and System.Linq namespace :
List<Book> list = new List<Book>();
return list.AsEnumerable();
This will, as said on this MSDN link change the type of the List in compile-time. This will give you the benefits also to only enumerate your collection we needed (see MSDN example for this).
IEnumerable<Book> _Book_IE;
List<Book> _Book_List;
If it's the generic variant:
_Book_IE = _Book_List;
If you want to convert to the non-generic one:
IEnumerable ie = (IEnumerable)_Book_List;
You need to
using System.Linq;
to use IEnumerable
options at your List
.
Why not use a Single liner ...
IEnumerable<Book> _Book_IE= _Book_List as IEnumerable<Book>;
Source: Stackoverflow.com