如何使用LINQ和EF6编写外连接?

时间:2014-03-25 10:43:49

标签: c# linq entity-framework

我有三个表Exam,Test和UserTest。

CREATE TABLE [dbo].[Exam] (
    [ExamId]                      INT            IDENTITY (1, 1) NOT NULL,
    [SubjectId]                   INT            NOT NULL,
    [Name]                        NVARCHAR (50)  NOT NULL,
    [Description]                 NVARCHAR (MAX) NOT NULL,
    CONSTRAINT [PK_Exam] PRIMARY KEY CLUSTERED ([ExamId] ASC),
    CONSTRAINT [FK_ExamSubject] FOREIGN KEY ([SubjectId]) REFERENCES [dbo].[Subject] ([SubjectId]),
    CONSTRAINT [FK_Exam_ExamType] FOREIGN KEY ([ExamTypeId]) REFERENCES [dbo].[ExamType] ([ExamTypeId])
);

CREATE TABLE [dbo].[Test] (
    [TestId]      INT            IDENTITY (1, 1) NOT NULL,
    [ExamId]      INT            NOT NULL,
    [Title]       NVARCHAR (100) NULL,
    [Status]      INT            NOT NULL,
    [CreatedDate] DATETIME       NOT NULL,
    CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED ([TestId] ASC),
    CONSTRAINT [FK_TestExam] FOREIGN KEY ([ExamId]) REFERENCES [dbo].[Exam] ([ExamId])
);

CREATE TABLE [dbo].[UserTest] (
    [UserTestId]              INT            IDENTITY (1, 1) NOT NULL,
    [UserId]                  NVARCHAR (128) NOT NULL,
    [TestId]                  INT            NOT NULL,
    [Result]                  INT            NULL
    CONSTRAINT [PK_UserTest] PRIMARY KEY CLUSTERED ([UserTestId] ASC),
    CONSTRAINT [FK_UserTestTest] FOREIGN KEY ([TestId]) REFERENCES [dbo].[Test] ([TestId])
);

考试可以进行多次测试,用户可以多次尝试任何测试。

如何使用扩展方法语法对LINQ语句进行编码,该语法允许我查看UserId == 1的以下内容(我假设Where子句中的UserId == 1):

Exam       Test      Title           UserTestID  UserId     Result
1          1         1a               1           1          20 
1          1         1a               2           1          30
1          1         1a               3           1          40         
1          2         1b               4           1          98 
1          3         1c               5           1          44
2          4         2a
2          5         2b               6           1          12

或者如果UserId == 2:

Exam       Test      Title           UserTestID  UserId     Result
1          1         1a               7           2          27  
1          2         1b        
1          3         1c               8           2          45
2          4         2a
2          5         2b        

或者,如果UserId为空

Exam       Test      Title           UserTestID  UserId     Result
1          1         1a        
1          2         1b
1          3         1c  
2          4         2a
2          5         2b   

请注意,由于我收到的建议,这个问题已经发生了一些变化。现在有一个赏金,我希望我能接受一个快速回答。

5 个答案:

答案 0 :(得分:4)

如果您的Test实体拥有UserTests集合,则可以使用此查询:

string userId = "1";
var result = context.Tests
    .SelectMany(t => t.UserTests
        .Where(ut => ut.UserId == userId)
        .DefaultIfEmpty()
        .Select(ut => new
        {
            ExamId = t.ExamId,
            TestId = t.TestId,
            Title = t.Title,
            UserTestId = (int?)ut.UserTestId,
            UserId = ut.UserId,
            Result = ut.Result
        }))
    .OrderBy(x => x.ExamId)
    .ThenBy(x => x.TestId)
    .ThenBy(x => x.UserTestId)
    .ToList();

在此处使用DefaultIfEmpty()可确保LEFT OUTER JOIN,以便您始终为给定的UserTest提供至少一个null实体(可能为Test)。将UserTest的非可空属性(如UserTestId)转换为可空类型(例如int?)非常重要,否则您可以获得{{1}的异常从数据库返回的值不能存储在不可为空的.NET类型中。

如果您不想在NULL实体中拥有UserTests个集合,那么您可以使用Test作为替代方案,基本上会留下外部联接GroupJoin的两个表:

TestId

答案 1 :(得分:1)

答案 2 :(得分:1)

 var tests = (from t in context.Tests
       // where !t.UsertTests.Any() //if no user took the test
         //    || t.UserTests.Any(ut=>ut.Student.StudentId == stId)
        select new {Test = t, Exam = t.Exam, 
                 UserTests = t.UserTests.Where(ut=>ut.Student.StudentId == stId))
       .ToList();

第二个想法,可能会更好。如果有任何匹配的或使用空的

,这将为您提供考试,测试和使用习惯

答案 3 :(得分:0)

以下是讨论的链接,该讨论显示了如何使用参数调用存储过程: How to use DbContext.Database.SqlQuery<TElement>(sql, params) with stored procedure? EF Code First CTP5

以下是编写存储过程的一种方法:

CREATE PROCEDURE dbo.sample1 (
@oneId NVARCHAR(128) = N'xx') AS
BEGIN
SET NOCOUNT ON;

SELECT @oneId AS userId,
    r.TestId, 
    r.Result
FROM (
    SELECT t.UserId, e.testId, t.Result
    FROM dbo.UserTest AS e
    LEFT OUTER JOIN dbo.UserTest AS t ON e.TestId = t.TestId AND t.UserId = @oneId
    WHERE  e.UserId = 0) AS r 
ORDER BY r.TestId 

END 
go

答案 4 :(得分:0)

试试这个:

var tests = context.Tests.Include( "Exam" )
    .Select( t => new
    {
        Test = t,
        UserTests = t.UserTests.Where( ut => ut.UserId == studentId )
    } )
    .ToList();