【问题标题】:Stored Procedure Multiple-Table Output With LINQ and ASP.NET MVC使用 LINQ 和 ASP.NET MVC 的存储过程多表输出
【发布时间】:2010-10-20 19:48:47
【问题描述】:

我们的系统经常调用存储过程来输出多个表的结果。以前,我们使用 XML 输出来获取每个表并使用 XSLT 正确关联它们。如果我使用 ASP.NET MVC 和 LINQ 调用存储过程,我如何获取每个表,然后根据需要输出数据?

【问题讨论】:

  • 您能否详细说明您的问题,您想在网页上显示存储过程中的这些数据吗?
  • 是的,我正在尝试使用 LINQ 在单个网页上输出三个独立的数据表。
  • Kezzer - 你还应该在 VB.NET 中询问答案并添加该标签,因为我知道你在 VB 世界中并且正在努力将 C# 转换为 VB.NET。

标签: asp.net-mvc linq stored-procedures


【解决方案1】:

this article here 解释了一切。这是我在您的previous SO question 中链接的同一篇文章。

【讨论】:

  • 我将此标记为正确,因为我在另一个问题中重新提出了这个问题,而您在此问题上花费了很多时间。干杯。
【解决方案2】:

这里有一篇关于 LINQ to SQL 和存储过程的文章,尤其是“处理 SPROC 中的多个结果形状”部分: LINQ to SQL - Retrieving Data Using Stored Procedures.

这对你有用吗?

否则,不使用LINQ to SQL,可能使用SqlDataReaderNextResult查看结果,例如:

IList<Employee> employees = new List<Employee>();
IList<Customer> customers = new List<Customer>();
using (SqlConnection connection = new SqlConnection
    (Properties.Settings.Default.NorthwindConnectionString))
using (SqlCommand command = new SqlCommand
    ("GetEmployeesAndCustomers", connection))
{
    command.CommandType = CommandType.StoredProcedure;
    connection.Open();
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            Employee e = new Employee{EmployeeID = (int)reader["EmployeeID"]};
            employees.Add(e);
        }
        reader.NextResult();
        while (reader.Read())
        {
            Customer c = new Customer{CustomerID = (string)reader["CustomerID"]};
            customers.Add(c);
        }
    }
}

编辑:如何处理不容易融入域模型对象的自定义数据组合的示例;在这种情况下,与订单的客户一起检索订单:

namespace Company.Application.ViewModel
{
    public class CustomerOrder
    {
        public string CustomerID { get; set; }
        public string CustomerName { get; set; }
        public int OrderID { get; set; }
        public DateTime? OrderDate { get; set; }
    }
}
namespace Company.Application.Repository
{
    public class CustomerOrderRepository
    {
        public IList<CustomerOrder> GetCustomerOrders()
        {
            NorthwindDataContext db = new NorthwindDataContext();
            var custorders = from customer in db.Customers
                             join order in db.Orders
                             on customer.CustomerID equals order.CustomerID
                             select new CustomerOrder
                             {
                                 CustomerID = customer.CustomerID,
                                 CustomerName = customer.CompanyName,
                                 OrderID = order.OrderID,
                                 OrderDate = order.OrderDate
                             };
            return custorders.ToList();
        }
    }
}

对此的启发:在chapter 关于NerdDinner 中,Scott Guthrie 谈到了创建自定义“ViewModel”对象来保存来自例如连接的数据,这些数据不容易适合域模型对象。

【讨论】:

  • 我读过那篇文章,但它没有解释如何使用 LINQ 在存储过程中恢复多个结果集。此外,它甚至没有解释在带回自定义数据集时如何使用 LINQ,即来自使用 JOIN 的查询的子集或表组合。不过,我还没有尝试过你的例子。
  • 关于带回自定义数据集,在这里 (aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf) 关于 Nerddinner (weblogs.asp.net/scottgu/archive/2009/03/10/…) 的章节中,Scott Guthrie 谈到了创建自定义“ViewModel”对象来保存来自例如连接的数据不容易适应域模型对象(我将在上面的编辑答案中举一个例子)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 2019-09-16
  • 2017-10-10
  • 1970-01-01
相关资源
最近更新 更多