我有這樣的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