我有这样的SQL查询:实体框架使用LINQ,INNER JOIN,GROUP BY排序方法的语法
SELECT
COUNT(PID.pid) AS Counted,
(SELECT TOP 1 Collect_id
FROM PID_Control
WHERE pid_controlY.org_pid = PID_Control.pid) as Collect_id
FROM
PID
INNER JOIN
PID_Control AS pid_controlY ON PID.pid = pid_controlY.pid
GROUP BY
pid_controlY.Collect_id, pid_controlY.org_pid
我想写Linq中的一样,但我不知道该如何处理本声明:
,(select top 1 Collect_id from PID_Control where pid_controlY.org_pid =
PID_Control.pid) as Collect_id
我已经这样做了,??? =错线
var list = db.PID_Control
.Include(t => t.PID1)
.GroupBy(k => new { k.Collect_id })
.Select(c => new Grouped
{
Collect_id = ???
Counted = c.Select(q => q.PID1.pid1).Count(),
})
.ToList();
如何在Linq中做到这一点?
类
public partial class PID_Control
{
public int pid_control_id { get; set; }
public Nullable<int> pid { get; set; }
public Nullable<int> Collect_id { get; set; }
public virtual PID PID1 { get; set; }
}
public partial class PID
{
public PID()
{
this.PID_Control = new HashSet<PID_Control>();
}
public int pid1 { get; set; }
public virtual ICollection<PID_Control> PID_Control { get; set; }
}
请添加两个实体类和类的DbContext这里也 –
增加了他们现在 – user4144972