首页 > 解决方案 > 内部加入 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; }
}

标签: c#linqasp.net-core

解决方案


如果我理解正确,表是通过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    
   };

推荐阅读