将sql查询转换为LINQ语句

时间:2015-09-15 14:36:29

标签: c# sql linq

我正在努力将这个sql语句翻译成LINQ语句。

select r.FirstName + ' ' + r.Surname as [Name], count(*) as  [Number of Submissions]
from Researcher r
join ResearcherSubmission rs on r.ResearcherID = rs.ResearcherID
join Submission s on s.SubmissionID = rs.SubmissionID
group by r.FirstName, r.Surname

这是我到目前为止所得到的,但我可能完全错了,因为我很难写LINQ语句

from r in Researcher
join rs in ResearcherSubmission on rs.ID equal s in Submission
count s in submission
group by r FirstName + ' ' + Surname

SQL查询确实为我提供了我正在寻找的结果。

1 个答案:

答案 0 :(得分:2)

您的SQL查询可以这样翻译:

var query= from r in db.Researchers
           join rs in db.ResearcherSubmissions on r.ResearcherID equals rs.ResearcherID
           join s in db.Submissions  on s.SubmissionID equals rs.SubmissionID
           group r by new {r.FirstName, r.Surname} into g
           select new {
                       Name= g.Key.FirstName + ' ' + g.Key.Surname,
                       NumberOfSubmissions= g.Count() 
                      };

return Json(query.ToList(), JsonRequestBehavior.AllowGet);

更新

如果您使用的是Entity Framework,那么如果您使用导航属性而不是连接,则此查询可能会更容易。看到您的SQL查询我注意到ResearchersSubmissions之间存在多对多的关系。假设你有这样的模型:

public class Researcher
{
   public Researcher 
   {
     ResearcherSubmissions=new List<ResearcherSubmission>();
   }
   public int ResearcherId{get;set;}
   public string FirstName{get;set;}
   public string Surname{get;set;}
   public virtual ICollection<ResearcherSubmission> ResearcherSubmissions{get;set;}
}

public class ResearcherSubmission
{
    [Key, ForeignKey("Researcher"), Column(Order=1)]
    public int ResearcherId{get;set;}
    [Key, ForeignKey("Submission"), Column(Order=2)]
    public int SubmissionId {get;set;}

    public virtual Researcher Researcher{get;set;}
    public virtual Submission Submission {get;set;}
}

public class Submission
{
   public Submission
   {
     ResearcherSubmissions=new List<ResearcherSubmission>();
   }
   public int SubmissionId{get;set;}

   public virtual ICollection<ResearcherSubmission> ResearcherSubmissions{get;set;}
}

您的查询将是:

 var query= db.Researchers
              .Where(r=>r.ResearcherSubmissions.Count>0)// Researchers that have submissions
              .Select(r=>new {
                               Name= r.FirstName + ' ' + r.Surname,
                               NumberOfSubmissions= r.ResearcherSubmissions.Count 
                             }
                     );
 return Json(query.ToList(), JsonRequestBehavior.AllowGet);

我上面展示的解决方案是映射联结表(ResearcherSubmissions),但是如果该表只包含外键而没有其他列,则该表可以由EF抽象,并且双方得到一个导航属性暴露另一方的集合(如果您想了解有关该主题的更多信息,请查看此link):

public class Researcher
{
   public Researcher 
   {
     Submissions=new List<Submission>();
   }
   public int ResearcherId{get;set;}
   public string FirstName{get;set;}
   public string Surname{get;set;}
   public virtual ICollection<Submission> Submissions{get;set;}
}

public class Submission
{
   public Submission
   {
     Researchers=new List<Researcher>();
   }
   public int SubmissionId{get;set;}

   public virtual ICollection<Researcher> Researchers{get;set;}
}

您的查询可能是这样的:

 var query= db.Researchers
              .Where(r=>r.Submissions.Count>0)
              .Select(r=>new {
                               Name= r.FirstName + ' ' + r.Surname,
                               NumberOfSubmissions= r.Submissions.Count 
                             }
                     );
 return Json(query.ToList(), JsonRequestBehavior.AllowGet);