Skip to content

note Select is used to project elements into a sequence #37178

@smiller7812

Description

@smiller7812

Description

In the section: Loading related data with Select,

I am not sure how the Select function really fits into the context of the differences between eager and lazy loading. (I've always been a little fuzzy here. I just always use eager loading when I need a nav prop filled)

But if this example should go here I would explain the fact that the Selection function being used here projects elements of a sequence into a new form be it a view model or a var variable capable of holding an anonymous type.

Your View Model projection example:

public IList CourseVM { get; set; }

public async Task OnGetAsync()
{
    CourseVM = await _context.Courses
    .Select(p => new CourseViewModel
    {
        CourseID = p.CourseID,
        Title = p.Title,
        Credits = p.Credits,
        DepartmentName = p.Department.Name
    }).ToListAsync();
}

My inline anonymous object example:

 var coursesProjected = await _context.Courses
 .Select(p => new 
 {
     CourseID = p.CourseID,
     Title = p.Title,
     Credits = p.Credits,
     DepartmentName = p.Department.Name
 }).ToListAsync();

coursesProjected has to be of type var here to work.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/read-related-data?view=aspnetcore-10.0&tabs=visual-studio

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/data/ef-rp/read-related-data.md

Document ID

3cb264da-2ff1-1256-572f-aa4ebca6e255

Platform Id

5d8ba75a-678a-21ac-469c-05e8afe24128

Article author

@tdykstra

Metadata

  • ID: d0026f69-7e5d-0d3e-5586-16ecc0656ff0
  • PlatformId: 5d8ba75a-678a-21ac-469c-05e8afe24128
  • Service: aspnet-core
  • Sub-service: data-access

Related Issues

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions