【问题标题】:How can i create one list from tow related table如何从两个相关表中创建一个列表
【发布时间】:2020-05-14 06:43:09
【问题描述】:

您好,我有一个带有 to table 的模型和一个一对多的关系 订单和状态

我想显示一个列表,其中包含所有属性的顺序和状态中的最后一个标题,根据 Order 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.net asp.net-mvc linq model-view-controller


【解决方案1】:

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

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

【讨论】:

  • 什么是我的问题的最佳连接查询我想显示订单中的所有数据以及状态表中的最后一个或第一个值谢谢
  • SELECT O.OrdeId,S.StatusId FROM Status s RIGHT OUTER JOIN order O ON (O.OrdeId=S.OrderID) where S.StatusId = (Select top 1 StatusId from Status ORDER BY S.状态IDESC)
  • 它在 sql sever 中使用而不是在 C# 中很有用,但我需要一个 linq 代码才能在 asp.net mvc 中使用
  • 您可以在 (var query = your query var Lists = db.Database.SqlQuery(query);) 在 mvc 中使用此查询或在 dapper 中使用它
猜你喜欢
  • 2014-06-29
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 2011-10-29
  • 2020-02-16
  • 1970-01-01
  • 2018-06-20
相关资源
最近更新 更多