如何使用EF 6.1进行外连接?

本文关键字:连接 何使用 EF | 更新日期: 2023-09-27 18:10:37

我有以下内容:

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

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

如何使用EF 6.1进行外连接?

试试这个:

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