Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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
C# EF Core带计数的左连接_C#_Mysql_Entity Framework_.net Core_Linq To Entities - Fatal编程技术网

C# EF Core带计数的左连接

C# EF Core带计数的左连接,c#,mysql,entity-framework,.net-core,linq-to-entities,C#,Mysql,Entity Framework,.net Core,Linq To Entities,我在MySql数据库上有3个表。我想在这3个表之间进行左连接,并使用group by进行计数 城市表 身份证 名字 学校表 身份证 CityId 名字 学生表 身份证 学生 名字 使用EF Core,我会这样尝试: ***Complex Class: CityWithStudentCount *** public int Id { get;set; } public string CityName { get;set; } public int StudentCount { get;set

我在MySql数据库上有3个表。我想在这3个表之间进行左连接,并使用group by进行计数

城市表
身份证
名字

学校表
身份证
CityId
名字

学生表
身份证
学生
名字

使用EF Core,我会这样尝试:

***Complex Class: CityWithStudentCount *** 
public int Id { get;set; }
public string CityName { get;set; }
public int StudentCount { get;set; }
Ef核心:

var db = new MyDbContext();

var result = (from city in db.City
             join school in db.School on city.Id equals school.CityId into t1
             from r1 in t1.DefaultIfEmpty()

             join student in db.Student on school.Id equals student.SchoolId into t2
             from r2 in t2.DefaultIfEmpty()

             select new CityWithStudentCount
             {
                 Id = city.Id,
                 CityName = city.Name,
                 StudentCount = t2.count()
             } into s1

             group s1 by s1.Id)
             .Select(s=>s.ToList())
             .ToList();
结果必须是这样的:

1 City1 10
2 City2 3
3 City3 0
4 City4 0
5 City5 12
如何使用Entity Framework Core查询此结果。谢谢。

您的查询是错误的

 var result = (from city in db.City
         join school in db.School on city.Id equals school.CityId into t1
         from school in t1.DefaultIfEmpty()

         join student in db.Student on school.Id equals student.SchoolId into t2
         from student in t2.DefaultIfEmpty()

         group student by new { city.Id,city.Name } into cityGrouped
         select new CityWithStudentCount
         {
             Id = cityGrouped.Key.Id,
             CityName = cityGrouped.Key.Name,
             StudentCount = cityGrouped.Count(x => x.student != null)
         }
         .ToList();

另外,我强烈建议您使用导航属性,而不是手动构建联接。

使用
城市.学校等。这些属性可以是
分组学生,因为您不需要
城市
进行计数。您可以从您定义的
by
进入
之间的键中获取这些属性。是的,你是对的。谢谢提醒。我更新了答案。@ASPMaker此答案已更新为我的建议。如果设置了导航属性,则可以使用导航属性而不是连接。类似于city.Schools.DefaultIfEmpty()
@ASPMaker中的学校的
,查询有问题吗?
 var result = (from city in db.City
         join school in db.School on city.Id equals school.CityId into t1
         from school in t1.DefaultIfEmpty()

         join student in db.Student on school.Id equals student.SchoolId into t2
         from student in t2.DefaultIfEmpty()

         group student by new { city.Id,city.Name } into cityGrouped
         select new CityWithStudentCount
         {
             Id = cityGrouped.Key.Id,
             CityName = cityGrouped.Key.Name,
             StudentCount = cityGrouped.Count(x => x.student != null)
         }
         .ToList();