简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM