我有两张桌子(1:N)
CREATE TABLE master (idMaster int identity (1,1) not null,
TheName varchar( 100) null,
constraint pk_master primary key(idMaster) clustered)
和 -
CREATE TABLE lnk (idSlave int not null,
idMaster int not null,
constraint pk_lnk_master_slave(idSlave) primary key clustered)
Master.idMaster和lnk.idMaster
之间的链接我有一个SQL查询:
select max (master.idMaster) as idMaster,
master.theName,
count (lnk.idSlave) as freq
from lnk
inner join master ON lnk.idMaster = master.idMaster
Group by master.theName
order by freq desc, master.theName
我需要将此T-SQL查询转换为Linq-to-SQL语句,最好是在C#
中答案 0 :(得分:0)
您无法控制Linq-to-SQL查询的确切sql输出,但这应该可以解决问题:
var results = from slave in dataContext.lnks
group slave by slave.idMaster
into slaveByMaster
let count = slaveByMaster.Count()
orderby count
select new
{
slaveByMaster.Key,
count,
};
您仍然需要其他查询来计算max(master.idMaster)。像这样:
var result2 = (from master in dataContext.masters
select master.idMaster).Max();