我經歷了很多關於Stackoverflow的問題,恐怕我仍然無法找到答案。我正在使用實體框架。使用LINQ我試圖在過去七天內找到最受歡迎的課程。我有參與查詢,日程,會議,地點四個表和聯接表ClientSesions使用LINQ獲得連接表中的最大數量
日程表的型號是
public class Schedule{
public Guid ScheduleID { get; set; }
public Guid CompanyID { get; set; }
public Guid LocationID { get; set; }
public Guid SessionID { get; set; }
public Guid SessionTypeID { get; set; }
public Guid ParentScheduleID { get; set; }
public int ClassDay { get; set; }
public int ClassMonth { get; set; }
public int ClassYear { get; set; }
public string Day { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public DateTime ClassDate { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public bool Deleted { get; set; }
public int SessionSize { get; set; }
public virtual Session Session { get; set; }
public virtual Company Company { get; set; }
public virtual Location Location { get; set; }
public virtual SessionType SessionType { get; set; }
}
會話模式
public class Session {
public System.Guid SessionID { get; set; }
public System.Guid ProgramID { get; set; }
public System.Guid CompanyID { get; set; }
public Nullable<Guid> SessionTypeID { get; set; }
public Int32 SessionSize { get; set; }
public bool Display { get; set; }
public virtual ClientSession ClientSession { get; set; }
}
的交界處的ClientSession表
public class ClientSession
{
public Guid ClientID { get; set; }
public Guid CompanyID { get; set; }
public Guid SessionID { get; set; }
public Nullable<System.Guid> TransactionID { get; set; }
// Navigation Properties
public virtual Client Client { get; set; }
public virtual Session Session { get; set; }
}
我寫了下面的SQL代碼按預期工作,但我擔心我只是沒有足夠的知識或經驗將其轉換爲Linq。
SELECT TOP 1 s.ClassDate, l.LocationName, COUNT(c.SessionID) as num_att
FROM Schedules s
JOIN Sessions ss ON s.SessionID = ss.SessionID
JOIN Sessions ss ON s.SessionID = ss.SessionID
JOIN ClientSessions c ON ss.SessionID = c.SessionID
JOIN Locations l ON s.LocationID = l.LocationID
WHERE s.CompanyID = '109'
AND s.LocationID = '4'
AND (s.ClassDate >= DATEADD(DAY, -7, GETDATE()))
GROUP BY s.ClassDate, l.LocationName
ORDER BY num_att DESC, s.ClassDate DESC
這是據我設法得到
var details = _repositoryBase.AllIncluding<Schedule>(x => x.Session, x => x.Location)
.Where(x => x.CompanyID == mostPopularSessionRequest.CompanyID
&& x.LocationID == mostPopularSessionRequest.LocationID
.GroupBy(x => x.ClassDate)
任何幫助,在向我展示如何獲得此完成了其餘的將非常感激。
在此先感謝您提供任何幫助。
是'_repositoryBase.AllIncluding(X => x.Session,X => x.Location)'已經照顧所有在SQL語句中加入? –
Marco
Hey Serv,是的,所有的連接都在AllIncluding中處理。 – CByrne
這是一個存儲庫代碼:public virtual IQueryable AllIncluding (params Expression > [] includeProperties)其中,TEntity:class { IQueryable query = DataContext.Set (); (包括includeProperties中的includeProperty){ query = query.Include(includeProperty); } 返回查詢; } –
CByrne