dotnet / dotnet/AspNetCore.Docs

note Select is used to project elements into a sequence

Open
#37,178 3 comments 0 reactions 1 assignee Claimed by @tdykstra View on GitHub
aspnet-core/svc data-access/subsvc Source - Docs.ms
Dominant language
C#
Stars
13.1k
Forks
24.6k
Avg merge
1d 3h
Merged PRs (30d)
97

Description

### 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](https://github.com/dotnet/AspNetCore.Docs/issues?q=is%3Aissue+is%3Aopen+3cb264da-2ff1-1256-572f-aa4ebca6e255)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.