hncl
hncl

Reputation: 2305

IEnumerable from a View c# MVC

I am not very experienced in SQL. I am using SQL Server 2008, and MVC3; have a simple view in my Database, trying to make a list using the following script:

 List<vw_LearnerCourse> list = ctx.vw_LearnerCourses.Where(x => x.Course_ID == id).ToList().AsEnumerable();

I get the following error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<vw_LearnerCourse>' to 'System.Collections.Generic.List<vw_LearnerCourse>'. An explicit conversion exists (are you missing a cast?)

Would appreciate your assistance. Thanks in advance.

Upvotes: 0

Views: 470

Answers (3)

John Koerner
John Koerner

Reputation: 38087

Call .ToList() on the IEnumerable.

List<vw_LearnerCourse> list = ctx.vw_LearnerCourses.Where(x => x.Course_ID == id).ToList();

From the documentation:

The ToList(IEnumerable) method forces immediate query evaluation and returns a List that contains the query results. You can append this method to your query in order to obtain a cached copy of the query results.

Upvotes: 1

Mitch Wheat
Mitch Wheat

Reputation: 300769

Just use:

 List<vw_LearnerCourse> list 
          = ctx.vw_LearnerCourses.Where(x => x.Course_ID == id).ToList();

Upvotes: 1

Naga Sandeep
Naga Sandeep

Reputation: 1431

You can see that List is defined as

[SerializableAttribute]
public class List<T> : IList<T>, ICollection<T>, 
IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, 
IEnumerable

by default List is Enumerable. So it works fine if you end the statement with ToList(). NO need of AsEnumerable().

List<vw_LearnerCourse> list = ctx.vw_LearnerCourses.Where(x => x.Course_ID == id).ToList();

Upvotes: 0

Related Questions