mardi 18 août 2015

How can I join in other tables to my LINQ statement and jget selected columns?

I created this linq statement to get a list of AdminTestQuestions:

var adminTests = await db.AdminTests
        .Include(t => t.AdminTestQuestions)
        .Where(t => t.ExamId == examId || examId == 0)
        .Where(t => t.TestStatusId == testStatusId || testStatusId == 0)
        .ToListAsync();
        return Ok(adminTests);

The statement works but I need to add in more information.

What I would like to do is to also get the Title of the question from the Question table and the SubTopidId of the question from the problem table

Can someone tell me how I can extend my linq statement to do this. What's confusing me is how I can stop linq getting all of the Question and Problem table columns.

CREATE TABLE [dbo].[AdminTest] (
    [AdminTestId]  INT            IDENTITY (1, 1) NOT NULL,
    [Title]        NVARCHAR (100) NOT NULL,

CREATE TABLE [dbo].[AdminTestQuestion]

    [AdminTestQuestionId] INT              IDENTITY (1, 1) NOT NULL,
    [AdminTestId]         INT              NOT NULL,
    [QuestionUId]         UNIQUEIDENTIFIER NOT NULL,

CREATE TABLE [dbo].[Question] (
    [QuestionId]       INT              IDENTITY (1, 1) NOT NULL,
    [ProblemId]        INT              NOT NULL,
    [QuestionUId]      UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [Title]            NVARCHAR (100)   NULL,
    [Grade]            INT              NOT NULL,

CREATE TABLE [dbo].[Problem] (
    [ProblemId]       INT             IDENTITY (1, 1) NOT NULL,
    [SubTopicId]      INT             NOT NULL,
    [Title]           NVARCHAR(20)    NULL

Aucun commentaire:

Enregistrer un commentaire