[英]Inner Join with linq and razor
我有 2 張桌子,PostThr 和 PostEig。 PostThr 具有服務的作業日期,而 PostEig 具有該作業的可用工作槽。
我想顯示即將到來的工作列表,並在每個工作下顯示可用的工作槽。 我怎么能這樣做?
據我了解,我應該編寫一個 linq 查詢,但我不確定如何編寫查詢。 我可以在下面分別為每個表編寫查詢。 在 sql 中,我會將其寫為各種連接。 然后,一旦我有了數據,我就可以在 razor 頁面上的 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
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.