Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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# 在linq中指定具有左外部联接的计数列_C#_Sql_Linq_Linq To Entities_Left Join - Fatal编程技术网

C# 在linq中指定具有左外部联接的计数列

C# 在linq中指定具有左外部联接的计数列,c#,sql,linq,linq-to-entities,left-join,C#,Sql,Linq,Linq To Entities,Left Join,我使用EF进行代码优先和迁移。我有两个实体: 公共类许可软件 { 公共int Id{get;set;} public int ResourceId{get;set;} public int SoftwareDetailId{get;set;} } 公共类软件详细信息 { 公共int Id{get;set;} 公共字符串FamilyName{get;set;} 公共字符串SoftwareName{get;set;} 公共字符串版本{get;set;} } 我试图在执行左外部联接时指定要计数的列,

我使用EF进行代码优先和迁移。我有两个实体:

公共类许可软件
{
公共int Id{get;set;}
public int ResourceId{get;set;}
public int SoftwareDetailId{get;set;}
}
公共类软件详细信息
{
公共int Id{get;set;}
公共字符串FamilyName{get;set;}
公共字符串SoftwareName{get;set;}
公共字符串版本{get;set;}
}
我试图在执行左外部联接时指定要计数的列,但它似乎只使用
count(*)

这是我的linq:

软件详细信息
.GroupJoin(许可软件,l=>l.Id,c=>c.SoftwareDetailId,
(l,c)=>
新{Licensed=l,Details=c.DefaultIfEmpty()})
.SelectMany(x=>x.Details,(x,merged)=>new{Details=merged,x.Licensed})
.GroupBy(x=>x.许可,x=>x.详细信息,
(键,g)=>
新{Licensed=key,Count=g.Count()})
生成的SQL是:

选择COUNT(*)作为[COUNT]
,[t0].[Id],[t0].[FamilyName],[t0].[SoftwareName],[t0].[Version]
从[SoftwareDetails]到[t0]
在[t0].[Id]=[t1].[SoftwareDetailId]上将[LicensedSoftwares]作为[t1]左外部联接
按[t0].[Id]、[t0].[FamilyName]、[t0].[SoftwareName]、[t0].[Version]分组
这也将计算SoftwareDetails表中的行,因此我将始终至少有1个计数

我希望它是(注意选择中的计数):

选择计数([t1].[Id])作为[COUNT]
,[t0].[Id],[t0].[FamilyName],[t0].[SoftwareName],[t0].[Version]
从[SoftwareDetails]到[t0]
在[t0].[Id]=[t1].[SoftwareDetailId]上将[LicensedSoftwares]作为[t1]左外部联接
按[t0].[Id]、[t0].[FamilyName]、[t0].[SoftwareName]、[t0].[Version]分组
我得到的最接近正确结果是:

软件详细信息
.选择(sd=>new
{ 
sd.Id、sd.FamilyName、sd.SoftwareName、sd.Version、,
计数=许可软件
.Where(ls=>ls.SoftwareDetailId==sd.Id&&ls.Id!=null)
.Count()
})
但我不喜欢这样生成的SQL:

选择[t0].[Id]、[t0].[FamilyName]、[t0].[SoftwareName]、[t0].[Version](
选择计数(*)
从[LicensedSoftwares]到[t1]
其中[t1].[SoftwareDetailId]=[t0].[Id]
)作为[计数]
从[SoftwareDetails]到[t0]

有人知道让EF使用计数中的特定列而不是所有列吗?

离题问题:SoftwareDetail不应该是子级,LicensedSoftware不应该是父级,所以forieng键应该是相反的

与我的问题无关,请尝试以下方法:

 var query = from softwareDetail in context.SoftwareDetails
                        join licensedSoftware in context.LicensedSoftwares
                            on softwareDetail.Id equals licensedSoftware.SoftwareDetailId
                        select new { softwareDetail, licensedSoftware }
                            into temp
                            group temp by temp.softwareDetail into temp2
                            select new { temp2, count = temp2.Count() };

不幸的是,这不会生成所需的sql。这也做了一个
内部连接
。你告诉我确切的SQL语句,我帮你翻译成LINQ查询,怎么样。我陈述了生成的sql,然后是我想要替换的sql。当我回到计算机上时,我会更新帖子。如果您执行左连接并想知道有多少成功(即,非空)的左连接结果,那么您执行内部连接和计算整个结果集(这就是我的LINQ所做的)之间的区别是什么?