如何将一个结果计入一个具有不同条件的组中? Linq查询

时间:2015-04-21 12:16:05

标签: sql linq count

 select 
        count(pat.ID) as PatientEmail,
        count(a.ID) as AlaEmails,
        count(t.ID) as TrusteeEmails,
        count(s.ID) + 1 as SpecialistEmail
    from [dbo].[Users] as u
    left join [dbo].[Patients] as pat
    on u.ID = pat.ID and u.Email != 'email@gmail.com'
    left join [dbo].[Patients] as a
    on u.ID = a.ID and u.Email = 'email@gmail.com'
    left join [dbo].[Trusteehips]as t
    on u.ID = t.Trustee_ID
    left join [dbo].[Specialists] as s
    on u.ID = s.ID and u.Email != 'email@gmail.com'
    where u.Active = 1 and u.Deleted = 0

    public class UserEmails
    {   
        public int PatientEmail { get; set; }
        public int AlaEmails { get; set; }
        public int TrusteeEmails { get; set; }

    }

public ActionResult ReportByEmail()
        {
            var res =  from u in context.Users
                       join p in context.Patients  
                       on u.ID equals p.ID into pGroup
                       join t in context.Trusteeships
                       on u.ID equals t.Trustee.ID into tGroup
                       select new UserEmails
                       {
                           PatientEmail = pGroup.Count(g => g.Email != "email@gmail.com"),
                           AlaEmails = pGroup.Count(g => g.Email = "email@gmail.com"),
                           TrusteeEmails = tGroup.Count()
                       };

            return View(res);

我试着写这个Linq代码,但它不起作用! 我的计数结果没有显示在一行,我用相同的语法编写代码(Group.Count()),它工作正常,但在这里它没有工作!

0 个答案:

没有答案