MVC如何计算相关模型中的记录?

时间:2017-03-28 10:27:02

标签: c# asp.net-mvc entity-framework asp.net-mvc-4

我有两个模型,第一个是关系,用Date打开,第二个模型是预订,现在我需要计算已经选择关系日期的预订记录。这些表位于DatumRID第二个表中第一个i记录的Relationship relID中。

如何计算预订中与ID相关的记录

模型关系:

  public tbl_relacii()
        {
            tbl_rezervacii = new HashSet<tbl_rezervacii>();
        }
        [Key]
        public int relID { get; set; }
        [Column(TypeName = "date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime DatumR { get; set; }
        public int sedista { get; set; }
        public string vozilo { get; set; }
        [StringLength(50)]
        public string shofer1 { get; set; }
        [StringLength(50)]
        public string shofer2 { get; set; }
        public string noteR { get; set; }
        public virtual ICollection<tbl_rezervacii> tbl_rezervacii { get; set; }
        public string DatumForDisplay
        {
            get
            {
                return DatumR.ToString("d");
            }
        }

模特预订:

 public partial class tbl_rezervacii
    {
        [Key]
        public int rID { get; set; }
        public int AgentID { get; set; }
        [StringLength(10)]
        public string karta_br { get; set; }
        public int DatumRID { get; set; }
        public int patnikID { get; set; }
        public int stanicaOD { get; set; }
        public int stanicaDO { get; set; }
        public decimal cena { get; set; }
        public bool povratna { get; set; }
        public DateTime? DatumP { get; set; }
        public string noteP { get; set; }
        public virtual tbl_agenti tbl_agenti { get; set; }
        public virtual tbl_patnici tbl_patnici { get; set; }
        public virtual tbl_relacii tbl_relacii { get; set; }
        public virtual tbl_stanici tbl_stanici { get; set; }
        public virtual tbl_stanici tbl_stanici1 { get; set; }
       public string relacija
        {
            get
            {
                return tbl_stanici.stanica + "=>" + tbl_stanici1.stanica;
            }
        }
        public string relacijaP
        {
            get
            {
                return tbl_stanici.stanica + "=>" + tbl_stanici1.stanica + "=>" + tbl_stanici.stanica;
            }
        }
    }

这里是Controller for Relations Index:

  public ActionResult Index()
        {
               return View(db.tbl_relacii.ToList().OrderByDescending(x => x.DatumR));
        }

如何计算预订中的记录,然后将记录数放入关系索引?

2 个答案:

答案 0 :(得分:0)

我认为你可以使用LINQ查询实现。请在下面找到示例。

var q = from d in Model.Reservations
        select new Relations
        {
            Count = d.Reservations.Count()
        };

答案 1 :(得分:0)

我简单地解决了。在关系模型中我添加了这个:

   public int Count
    {
        get
        {
            return tbl_rezervacii.Count;
        }
     }

问题解决了。谢谢