包括第二个 table Linq to Sql
Including a second table Linq to Sql
我有一个字典结果,它是 return输入的每个清单的计数。
类 看起来像这样:
public class COMPLAINT
{
[Key]
public int COMP_ID { get; set; }
public Nullable<DateTime> Received_DT { get; set; }
public virtual ICollection<CHECKLIST> CHECKLISTs { get; set; }
}
public class CHECKLIST
{
[Key]
public int CL_ID { get; set; }
public int EmpID { get; set; }
public int COMP_ID { get; set; }
}
当我只 return _context.Checklists 时,我得到了我想要 return 编辑的总计数,如下所示。
public Dictionary<int, int> GetAllComplaintsCount()
{
try
{
return _context.Checklists
.GroupBy(a => a.EmpID)
.ToDictionary(g => g.Key, g => g.Count());
}
catch (Exception ex)
{
_logger.LogError("Could not get am with checklist", ex);
return null;
}
}
问题
但是当我添加投诉 table 时,我得到一个错误,EmpID 不在投诉 table 中,但它在清单 table 中。如何包含投诉 table 并按 EmpID 分组,同时在接收日期之间进行查询?
public Dictionary<int, int> GetAllComplaintsCount()
{
try
{
return _context.Complaints
.Include(t=> t.Checklists)
.GroupBy(a => a.EmpID)
.ToDictionary(g => g.Key, g => g.Count());
}
catch (Exception ex)
{
_logger.LogError("Could not get am with checklist", ex);
return null;
}
}
清单上仍然缺少您的投诉 属性,但假设这是另一个疏忽,我认为这就是您想要的:
public Dictionary<int, int> GetAllComplaintsCount(DateTime start, DateTime finish)
{
try
{
return _context.Checklists
.Where(a=>a.Complaint.Received_DT>=start && a.Complaint.Received_DT<finish)
.GroupBy(a => a.EmpID)
.ToDictionary(g => g.Key, g => g.Count());
}
catch (Exception ex)
{
_logger.LogError("Could not get am with checklist", ex);
return null;
}
}
我有一个字典结果,它是 return输入的每个清单的计数。
类 看起来像这样:
public class COMPLAINT
{
[Key]
public int COMP_ID { get; set; }
public Nullable<DateTime> Received_DT { get; set; }
public virtual ICollection<CHECKLIST> CHECKLISTs { get; set; }
}
public class CHECKLIST
{
[Key]
public int CL_ID { get; set; }
public int EmpID { get; set; }
public int COMP_ID { get; set; }
}
当我只 return _context.Checklists 时,我得到了我想要 return 编辑的总计数,如下所示。
public Dictionary<int, int> GetAllComplaintsCount()
{
try
{
return _context.Checklists
.GroupBy(a => a.EmpID)
.ToDictionary(g => g.Key, g => g.Count());
}
catch (Exception ex)
{
_logger.LogError("Could not get am with checklist", ex);
return null;
}
}
问题 但是当我添加投诉 table 时,我得到一个错误,EmpID 不在投诉 table 中,但它在清单 table 中。如何包含投诉 table 并按 EmpID 分组,同时在接收日期之间进行查询?
public Dictionary<int, int> GetAllComplaintsCount()
{
try
{
return _context.Complaints
.Include(t=> t.Checklists)
.GroupBy(a => a.EmpID)
.ToDictionary(g => g.Key, g => g.Count());
}
catch (Exception ex)
{
_logger.LogError("Could not get am with checklist", ex);
return null;
}
}
清单上仍然缺少您的投诉 属性,但假设这是另一个疏忽,我认为这就是您想要的:
public Dictionary<int, int> GetAllComplaintsCount(DateTime start, DateTime finish)
{
try
{
return _context.Checklists
.Where(a=>a.Complaint.Received_DT>=start && a.Complaint.Received_DT<finish)
.GroupBy(a => a.EmpID)
.ToDictionary(g => g.Key, g => g.Count());
}
catch (Exception ex)
{
_logger.LogError("Could not get am with checklist", ex);
return null;
}
}