raklos
raklos

Reputation: 28545

linq to sql -get data from a table related to another

Im using linq to sql

i have a Documents table

and a FavouriteDocuments table

FavouriteDocuments table has a documentsID fk and a ProjectID fk.

given the ProjectID how do i get all the documents(from the documents table) that are a FavouriteDocument for that particular project.

thanks

Upvotes: 3

Views: 748

Answers (1)

Steven
Steven

Reputation: 172606

Try this:

public static Document[] GetFavouriteDocumentsForProject(int projectId)
{
    using (var db = new MyContext())
    {
        return
            (from favourite in db.FavouriteDocuments
            where favourite.ProjectID == projectId
            select favourite.Document).ToArray();
    }
}

I hope this helps.

Upvotes: 2

Related Questions