select
t.Ticketid, t.subject, t.createdby,
count(TicketConversationId) as counts
from
dbo.ticket t
join
dbo.TicketConversation tc on t.ticketid = tc.ticketid
group by
t.Ticketid, t.subject, t.createdby
代码:
var Result = from t1 in Tickets
join t2 in TicketConversations on t1.TicketId equals t2.TicketId
select new { t1.TicketId, t1.Subject , comments = t2.TicketConversationId };
答案 0 :(得分:1)
好的,要为查询添加计数和分组,请使用GroupBy
:
var result = from t1 in Tickets
join t2 in TicketConversations on t1.TicketId equals t2.TicketId
group t2.TicketConversationId by new { t.Ticketid, t.Subject, t.createdby } into g
select new {
t1.TicketId,
t1.Subject ,
t1.CreatedBy,
Count = g.Count() };
请注意,当您使用EF时,如果您正确定义了Navigation Properties,则无需显式编写连接,只需t1.Conversations.Count()
或类似。
您只需要计算匹配的t2
中的记录数,就可以使用group join
var result = from t1 in Tickets
join t2 in TicketConversations on t1.TicketId equals t2.TicketId into con
select new {
t1.TicketId,
t1.Subject,
t1.CreatedBy,
Count = con.Count()
};