I am trying to get the number of subjects each student have using the below query:
var selectedSubject = context.Students
.Include(d => d.Subjects)
.Select(dr => new
{
Name = dr.Student.FirstName,
NoOfSubject = dr.Subjects.Count
})
.ToList();
But I am getting an exception
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll
Additional information: The specified type member 'Subjects' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported
You won't be able to access non
entity member
Properties on theIList
interface (likeCount
) until the query is materialized.Either materialize early:
Or, make use of the
Count()
method, which will be mappable into Sql: