首页 > 解决方案 > 如何从两个相关表中创建一个列表

问题描述

嗨,我有表模型以及订单和状态之间的一对多关系

我想按顺序显示所有属性的列表,并根据订单 ID 显示状态中的最后一个标题,以便我如何获得它?谢谢你

这是我的代码:

    namespace Pur.Models
{
    using System;
    using System.Collections.Generic;


    public partial class Order
    {


        public int OrdeId { get; set; }
        public Nullable<int> UserID { get; set; }

        public Nullable<System.DateTime> OrderDate { get; set; }


        public string Description { get; set; }


        public int Quantity { get; set; }


        public string Unit { get; set; }


        public string Priority { get; set; }


        public string Department { get; set; }


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


    }
}

和另一个:

namespace Pur.Models
{
    using System;
    using System.Collections.Generic;


    public partial class Status
    {
        public int StatusId { get; set; }

        public string Title { get; set; }

        public int OrderID { get; set; }


        public Nullable<System.DateTime> StatusDate { get; set; }

        public virtual Order Order { get; set; }
    }

标签: asp.netasp.net-mvclinqmodel-view-controller

解决方案


您可以创建连接查询,然后将其传递给新模型

var query = your query
var Lists = db.Database.SqlQuery<OrderModel>(query);

推荐阅读