Max
Max

Reputation: 5063

In MVC3 how to get a list of models with Linq?

In my database context class I have:

Imports System.Data.Entity
Public Class MusicStoreEntities
    Inherits DbContext
    Public Property Albums As DbSet(Of Album)
    Public Property Descriptions As DbSet(Of Description)
End Class

Then, I have those two models for Album and Description

Public Class Album
    Public Property AlbumId As Integer
End Class

Public Class Description
    Public Property DescriptionId As Integer
    Public Property AlbumId As Integer
End Class

From an Action of a Controller I can get a single Description for an Album using Linq as the following:

Dim album_description As Description = db.Descriptions.Single(Function(g) g.AlbumId = id)

However, how can I get a list of Descriptions for an Album?

Dim album_descriptions As List(Of Description) = db.Descriptions.**???**(Function(g) g.AlbumId = id)

Upvotes: 0

Views: 186

Answers (2)

DevDave
DevDave

Reputation: 6908

  Dim album_descriptions As List(Of Description) = db.Descriptions.Where(Function(g) g.AlbumId = id).ToList() 

should do it

Upvotes: 2

Michal Klouda
Michal Klouda

Reputation: 14521

Use the .Where() method, documented here.

db.Descriptions.Where(Function(g) g.AlbumId = id).ToList()

Upvotes: 1

Related Questions