如何使用EF 6.1进行外连接

时间:2014-06-27 12:10:46

标签: c# entity-framework

我有以下内容:

 var tests = await db.Tests
            .Include(t => t.Exam)
            .Where(t => t.TestStatusId == 1)
            .Select(t => new TestDTO
            {
                ExamName = t.Exam.Name,
                Id = t.TestId,
                QuestionsCount = t.QuestionsCount,
                Title = t.Title
            })
            .ToListAsync();
        return Ok(tests);

即使特定测试没有匹配的考试,我怎样才能使它仍然返回测试?

1 个答案:

答案 0 :(得分:0)

试试这个:

//first step
    var tests = db.Tests
        .Include(t => t.Exam)
        .Where(t => t.TestStatusId == 1);

        //next step
        if(testc.Exam != null && testc.Exam.Count > 0)
        {
            testc = testc.Select(t => new TestDTO
        {
            ExamName = t.Exam.Name,
            Id = t.TestId,
            QuestionsCount = t.QuestionsCount,
            Title = t.Title
        });
        }