【问题标题】:Using LINQ to get Max Count in Junction Table使用 LINQ 获取连接表中的最大计数
【发布时间】:2014-05-21 08:21:51
【问题描述】:

我在 Stackoverflow 上遇到了很多问题,但恐怕我仍然找不到答案。我正在使用实体框架。使用 LINQ,我试图找到过去 7 天内最受欢迎的课程。我有四个表参与查询,Schedules、Sessions、Locations 和联结表ClientSessions

时间表的模型是

     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; }
}

Junction 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&lt;Schedule&gt;(x =&gt; x.Session, x =&gt; x.Location) 是否已经处理了 SQL 语句中的所有连接?
  • Hey Serv,是的,所有连接都在 AllIncluding 中处理。
  • 这是一个存储库代码: public virtual IQueryable AllIncluding(params Expression>[] includeProperties) where TEntity : class { IQueryable query = DataContext.Set(); foreach (var includeProperty in includeProperties) { query = query.Include(includeProperty); } 返回查询; }

标签: c# sql linq junction-table


【解决方案1】:
 var query = from s in Schedules
                        join ss in Schedules on s.SessionID equals ss.SessionID
                        join c in ClientSessions on ss.SessionID equals c.SessionID
                        join l in Locations on s.LocationID equals l.LocationID
                        where s.CompanyID =  109  && s.LocationID = 4 && (s.ClassDate >= DateTime.Now.AddDays(-7))
                         group c by new
                        {
                            c.ClassDate,
                            l.LocationName 
                        } into gcs
                        select new  
                        {
                            ClassDate = gcs.Key.ClassDate,
                            LocationName = gcs.Key.LocationName,
                            SessionIDCount = gcs.c.Count()
                        };

【讨论】:

  • 感谢 P John Raj。我会试一试,看看我会怎么走。我会告诉你。再次感谢您抽出宝贵时间回答。
  • 我不得不重新调整连接以适应我的数据访问,但分组和选择工作完美。谢谢。
  • 我注意到我身边的一个错误是我两次加入 Schedule
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 2015-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多