c# - 内部加入 linq 和 razor
问题描述
我有 2 张桌子,PostThr 和 PostEig。PostThr 具有服务的作业日期,而 PostEig 具有该作业的可用工作槽。
我想显示即将到来的工作列表,并在每个工作下显示可用的工作槽。我怎么能这样做?
据我了解,我应该编写一个 linq 查询,但我不确定如何编写查询。我可以在下面分别为每个表编写查询。在 sql 中,我会将其编写为各种连接。然后,一旦我有了数据,我就可以在剃须刀页面上的 foreach 循环中显示它们。
var today = DateTime.Now.Date;
var jobs = _context.PostThrs.Where(m => m.ThrDate > today
&& m.ThrText == "SERVICE DATE");
//zero is the FK...
var zero = "2102-01";
var slots = _context.PostEigs.Where(m => m.EigZero == zero
&& m.EigAgen == "OPEN");
2张表如下:
Table PostThr
ThrId | ZeroId | ThrDate | ThrText
Table PostEig
EigId | ZeroId | EigAgen | EigLoad
更新
主零表
public class PostZero
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Name = "0/")]
public string Zero { get; set; }
}
PostThr表
public class PostThr
{
[Key]
public int ThrId { get; set; }
[ForeignKey("PostZero")]
public string ThrZero { get; set; }
public int ThrDigit { get; set; }
[DataType(DataType.Date)]
public DateTime ThrDate { get; set; }
public string ThrTime { get; set; }
[Required]
public string ThrText { get; set; }
}
PostEig 表
public class PostEig
{
[Key]
public int EigId { get; set; }
[ForeignKey("PostZero")]
public string EigZero { get; set; }
[Display(Name = "Number")]
public int EigDigit { get; set; }
[Required]
public string EigAgen { get; set; }
[Required]
public string EigRole { get; set; }
public string EigCont { get; set; }
public decimal EigLoad { get; set; }
public string EigNote { get; set; }
}
用虚拟机更新
public class AgentClientIndexVM
{
public string Zero { get; set; }
public DateTime ThrDate { get; set; }
public string ThrTime { get; set; }
public string ThrText { get; set; }
public string EigAgen { get; set; }
public string EigRole { get; set; }
public decimal EigLoad { get; set; }
public string EigNote { get; set; }
}
解决方案
如果我理解正确,表是通过PostZero
var today = DateTime.Now.Date;
var jobs =
from h in _context.PostThrs
join e in _context.PostEigs on h.ThrZero equals e.EigZero
where h.ThrDate > today && h.ThrText == "SERVICE DATE"
&& e.EigAgen == "OPEN"
select new AgentClientIndexVM
{
Zero = h.ThrZero,
ThrDate = h.ThrDate,
ThrTime = h.ThrTime,
ThrText = h.ThrText,
EigAgen = e.EigAgen,
EigRole = e.EigRole,
EigLoad = e.EigLoad,
EigNote = e.EigNote
};
推荐阅读
- c - 使用循环双链表实现栈
- flutter - showModalBottomSheet 触发 Futurebuilder
- java - 使用最新的 java 版本重新编译旧的 java 项目的性能优势
- html - 如何从不同的 html 文件加载网页的不同部分
- javascript - GetMethod.getResponseBodyAsString() 到 xml 对象
- apache-spark - 火花连接性能:多列与单列
- javascript - Javascript - 从对象数组中删除重复的 ID
- java - getMethod什么时候需要锁?我尝试了几次在get方法中放置而不是锁定而没有看到变化
- reactjs - 反应路由器重定向在私有路由中不起作用
- python - 如何在python函数中传递具有属性值的对象