在LINQ to SQL中包含多个子层

时间:2016-09-21 16:01:55

标签: entity-framework linq-to-sql entity-framework-6

我有一个数据模型,它有三层如下:

  • 市场消息
  • MessageType598
  • 计量生成信息

课程如下:

[Table("MarketMessage")]
    public partial class MarketMessage
    {
        public MarketMessage()
        {
            messageType300 = new HashSet<messageType300>();
            messageType300S = new HashSet<messageType300S>();
            messageType300W = new HashSet<messageType300W>();
            messageType305 = new HashSet<messageType305>();
            messageType310 = new HashSet<messageType310>();
            messageType310W = new HashSet<messageType310W>();
            messageType320 = new HashSet<messageType320>();
            messageType320W = new HashSet<messageType320W>();
            messageType332 = new HashSet<messageType332>();
            messageType332W = new HashSet<messageType332W>();

            messageType591 = new HashSet<messageType591>();
            messageType594 = new HashSet<messageType594>();
            messageType595 = new HashSet<messageType595>();
            messageType596 = new HashSet<messageType596>();
            messageType597 = new HashSet<messageType597>();
            messageType598 = new HashSet<messageType598>();
        }

        public int MarketMessageID { get; set; }

        public DateTime CreatedOn { get; set; }

        [Required]
        [StringLength(4)]
        public string messageType { get; set; }

        [Required]
        [StringLength(8)]
        public string VersionNumber { get; set; }

        public DateTime MarketTimestamp { get; set; }

        [Required]
        [StringLength(35)]
        public string TxRefNbr { get; set; }

        [Required]
        [StringLength(3)]
        public string Sender { get; set; }

        [Required]
        [StringLength(3)]
        public string Recipient { get; set; }

        [StringLength(10)]
        public string alertFlag { get; set; }

        [StringLength(50)]
        public string fileName { get; set; }

        public bool IsDeleted { get; set; }

        public virtual ICollection<messageType300> messageType300 { get; set; }

        public virtual ICollection<messageType300S> messageType300S { get; set; }

        public virtual ICollection<messageType300W> messageType300W { get; set; }

        public virtual ICollection<messageType305> messageType305 { get; set; }

        public virtual ICollection<messageType310> messageType310 { get; set; }

        public virtual ICollection<messageType310W> messageType310W { get; set; }

        public virtual ICollection<messageType320> messageType320 { get; set; }

        public virtual ICollection<messageType320W> messageType320W { get; set; }

        public virtual ICollection<messageType332> messageType332 { get; set; }

        public virtual ICollection<messageType332W> messageType332W { get; set; }

        public virtual ICollection<messageType591> messageType591 { get; set; }

        public virtual ICollection<messageType594> messageType594 { get; set; }

        public virtual ICollection<messageType595> messageType595 { get; set; }

        public virtual ICollection<messageType596> messageType596 { get; set; }

        public virtual ICollection<messageType597> messageType597 { get; set; }

        public virtual ICollection<messageType598> messageType598 { get; set; }
    }


    public partial class messageType598 
    {
        public int messageType598ID { get; set; }

        public int MarketMessageID { get; set; }

        public DateTime SettlementDate { get; set; }

        public int SettlementRunIndicator { get; set; }

        [Required]
        [StringLength(9)]
        public string GenerationUnit { get; set; }
    }


    [Table("MeteredGenerationInformation")]
    public partial class MeteredGenerationInformation
    {
        [Key]
        public int MeteredGenerationInfoID { get; set; }

        public DateTime IntervalPeriodTimestamp { get; set; }

        public int SettlementInterval { get; set; }

        public decimal GenerationUnitMeteredGeneration { get; set; }

        public decimal LossAdjustedGenerationUnitMeteredGeneration { get; set; }

        public int? MessageType594ID { get; set; }

        public int? MessageType598ID { get; set; }
    }

我正在尝试加入598类型的所有市场消息列表,包括计量生成信息,但我不确定如何通过LINQ to SQL执行此操作。我尝试了以下方法:

var list598s = db.MarketMessage.Where(mm => mm.messageType == "598")
                .Include(mm => mm.messageType598)
                .ToList();

这包括市场消息,包括598信息,但如何在此列表中也包含计量生成信息,我还希望根据messageType598级别中找到的日期字段优化列表

作为SQL查询,我会写以下内容:

select * from MarketMessage as a
inner join messageType598 as b on a.MarketMessageID = b.MarketMessageID
inner join MeteredGenerationInformation as c on b.messageType598ID = c.messageType598ID
where c.IntervalPeriodTimestamp between '1 aug 2016' and '31 aug 2016'

0 个答案:

没有答案