这是我的表格定义:
Ticket
-------------------------------
|ID int PK |
|Paid varchar(50) |
-------------------------------
TicketRow
----------------------------------
|ID int PK |
|TicketID_FK int |
|SHtimeID_FK int |
----------------------------------
这是我的SQL查询:
select SHtimeID_FK,count(*) as cnt
from dbo.TicketRow as tr inner join dbo.Ticket as t on tr.TicketID_FK=t.ID
where t.Paid='ok'
group by SHtimeID_FK
having count(*)>1
我应该如何将此查询转换为Linq查询?
更新:
我尝试此查询,但我不知道应该如何完成查询!
MelliConcertEntities db = new MelliConcertEntities();
var s = (from x in db.TicketRow
where x.Ticket.Paid == "ok"
select x).GroupBy(???);
答案 0 :(得分:1)
db.TicketRow
.Where( tr => tr.Ticket.Paid == "ok" )
.GroupBy( tr => tr.SHtimeID_FK )
.Where( g => g.Count() > 1 )
.Select( g =>
new {
SHtimeID_FK = g.Key,
cnt = g.Count() } );
或
var s = from tr in db.TicketRow
where tr.Ticket.Paid == "ok"
group tr by tr.SHtimeID_FK into g
where g.Count() > 1
select new {
SHtimeID_FK = g.Key,
cnt = g.Count()
};