Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建执行左外部联接以及从另一个表进行计数的SQL?_Sql_Sql Server_Linq - Fatal编程技术网

如何创建执行左外部联接以及从另一个表进行计数的SQL?

如何创建执行左外部联接以及从另一个表进行计数的SQL?,sql,sql-server,linq,Sql,Sql Server,Linq,我有两个实体: public class AdminTest { public AdminTest() { this.AdminTestQuestions = new List<AdminTestQuestion>(); this.UserTests = new List<UserTest>(); } public int AdminTestId { get; set; } public string

我有两个实体:

public class AdminTest
{
    public AdminTest()
    {
        this.AdminTestQuestions = new List<AdminTestQuestion>();
        this.UserTests = new List<UserTest>();
    }
    public int AdminTestId { get; set; }
    public string Title { get; set; }
    public virtual ICollection<AdminTestQuestion> AdminTestQuestions { get; set; }
    public virtual ICollection<UserTest> UserTests { get; set; }
}

public UserTest()
    {
        this.UserTestQuestions = new List<UserTestQuestion>();
    }

    public int AdminTestId { get; set; }
    public int CreatedBy { get; set; }
    public int UserTestId { get; set; }
    public virtual AdminTest AdminTest { get; set; }
}
给予:

AdminTestId       Title        CreatedBy
1                 A            NULL
2                 B            99
我还有一个表格,列出了每个测试中的问题:

public partial class AdminTestQuestion
{
    public int AdminTestQuestionId { get; set; }
    public int AdminTestId { get; set; }
    public System.Guid QuestionUId { get; set; }
    public virtual AdminTest AdminTest { get; set; }

}
我如何修改SQL以添加到其他表AdminTestQuestions中,从而给出如下问题计数:

SELECT AdminTest.AdminTestId, AdminTest.Title, UserTest.CreatedBy FROM AdminTest
LEFT OUTER JOIN UserTest
ON AdminTest.AdminTestId = UserTest.AdminTestId
AdminTestId       Title        Questions  CreatedBy
1                 A            10         NULL
2                 B            20         99
我将Linq与Entity Framework 6结合使用,因此Linq或SQL解决方案将是不错的选择。

SQL

SELECT
    AdminTest.AdminTestId,
    AdminTest.Title,
    COUNT(AdminTestQuestion.AdminTestQuestionId) Questions,
    UserTest.CreatedBy
FROM
    AdminTest LEFT OUTER JOIN UserTest
ON
    AdminTest.AdminTestId = UserTest.AdminTestId JOIN AdminTestQuestion
ON
    AdminTest.AdminTestId = AdminTestQuestion.AdminTestId
GROUP BY
    AdminTest.AdminTestId, AdminTest.Title, UserTest.CreatedBy
林克

在某些情况下,“count(distinct leftKey)”或“sum(rightKey为null的情况下,leftValue为null end)”也可能是您要查找的内容。
var query = from at in db.AdminTests
            join ut in db.UserTests
            on at.AdminTestId equals ut.AdminTestId into at_uts
            from at_ut in at_uts.DefaultIfEmpty()
            select new
            {
                at.AdminTestId,
                at.Title,
                Questions = at.AdminTestQuestions.Count(),
                at_ut.CreatedBy
            };