私は Stackoverflow について多くの質問をしましたが、まだ答えが見つからないのではないかと心配しています。Entity Framework を使用しています。LINQ を使用して、過去 7 日間で最も人気のあるクラス セッションを見つけようとしています。クエリ、Schedules、Sessions、Locations、およびジャンクション テーブル ClientSessions に関連する 4 つのテーブルがあります。
スケジュールのモデルは
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)
この残りの部分を完成させる方法を教えていただけると助かります。
どんな助けでも前もって感謝します。