Linq to sql -get data from table related to another

Im using linq for sql

I have a table of documents

and FavoriteDocuments table

The FavoriteDocuments table has documentID fk and ProjectID fk.

given ProjectID, how can I get all the documents (from the document table) that are FavoriteDocument for this particular project.

thank

+3
source share
1 answer

Try the following:

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();
    }
}

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1731367/


All Articles