我有以下SQL查询,我需要使用导航属性重写到Entity Framework。基本上,员工有许多人可以自愿做的任务。我试图找到至少有一名志愿者被分配到任何任务的所有员工。
SELECT j.noTasks, e.*
FROM Employee e
LEFT JOIN
(SELECT e.id, COUNT(vt.TaskId) as "noTasks"
FROM Employee e
LEFT JOIN Task t on e.id = t.EmployeeId
LEFT JOIN VolunteerTask vt on vt.TaskId = t.id
GROUP BY e.id) j
ON e.id = j.id AND noTasks > 0
麻烦的部分是内部的SELECT。我已经阅读了文档,但是我很难理解它。
员工类:
public partial class Employee : IEntity
{
public int id { get; set; }
[Required]
[StringLength(100)]
public string Name { get; set; }
[StringLength(100)]
public string WorkPlace { get; set; }
public int EmployeeTypeId { get; set; }
[Required]
[StringLength(11)]
public string OrganizationNumber { get; set; }
[StringLength(20)]
public string Telephone { get; set; }
[Required]
[StringLength(100)]
public string StreetAddress { get; set; }
[StringLength(200)]
public string CoAddress { get; set; }
[Required]
[StringLength(20)]
public string ZipCode { get; set; }
[StringLength(200)]
public string City { get; set; }
[StringLength(200)]
public string HomePage { get; set; }
[Required]
[StringLength(200)]
public string ContactPerson { get; set; }
[StringLength(30)]
public string ContactPhone { get; set; }
[StringLength(30)]
public string ContactPhone2 { get; set; }
[StringLength(200)]
public string ContactEmailAddress { get; set; }
public DateTime RegisteredDate { get; set; }
public virtual EmployeeType EmployeeType { get; set; }
[Required]
public UserStatus Status { get; set; }
public string ImageFileName { get; set; }
}
任务类:
public partial class Task : IEntity
{
public Task()
{
VoluntaryTasks = new HashSet<VoluntaryTask>();
TaskInterest = new HashSet<TaskInterest>();
TaskCalendarItems = new HashSet<TaskCalendarItem>();
}
public int id { get; set; }
[StringLength(100)]
public string Name { get; set; }
public int EmployeeId { get; set; }
public int AreaId { get; set; }
[Column(TypeName = "ntext")]
public string Description { get; set; }
public int NoOfVolunteers { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? PublicationDate { get; set; }
public DateTime EndDate { get; set; }
[Required]
[StringLength(200)]
public string ContactPerson { get; set; }
[Required]
[StringLength(30)]
public string ContactTelephone { get; set; }
[StringLength(30)]
public string ContactTelephone2 { get; set; }
[StringLength(100)]
public string EmailAddress { get; set; }
public bool isPublished { get; set; }
public bool NotificationMailSent { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public int Priority { get; set; }
public RecordStatus RecordStatus { get; set; }
public virtual Area Area { get; set; }
public string LocationDescription {get; set;}
public virtual Employee Employee { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<VoluntaryTask> VoluntaryTasks { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TaskInterest> TaskInterest { get; set; }
public virtual ICollection<TaskLanguageSkill> TaskLanguageSkills { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TaskCalendarItem> TaskCalendarItems { get; set; }
}
VoluntaryTask类:
public partial class VoluntaryTask
{
//[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int VoluntaryId { get; set; }
//[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int TaskId { get; set; }
//[Key]
public VoluntaryTaskStatus Status { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public virtual Task Task { get; set; }
public virtual Voluntary Voluntary { get; set; }
}