简体   繁体   English

使用QueryMultiple时如何确定返回哪些记录?

[英]How to determine which records are returned when using QueryMultiple?

I'm retrieving records by doing the following:我正在通过执行以下操作来检索记录:

using (var multi = await connection.QueryMultipleAsync(procedureName, param, commandType: CommandType.StoredProcedure))
{
    var results = new List<dynamic>();

    while (!multi.IsConsumed)
    {
        results.Add(multi.Read().ToList());
    }

    return results;
}

So far so good.到现在为止还挺好。 The stored procedure returns the data by doing:存储过程通过执行以下操作返回数据:

SELECT * FROM @Orders;
SELECT * FROM @OrderDetails;

Is it possible to change the List<dynamic> to a Dictionary<string, IEnumerable<dynamic>> and set the key automatically when reading the GridReader , so that in the above example, the dictionary will have two entries with the keys Orders and OrderDetails .是否可以将List<dynamic>更改为Dictionary<string, IEnumerable<dynamic>>并在读取GridReader时自动设置键,以便在上面的示例中,字典将有两个条目,键为OrdersOrderDetails .

Using Read<T> is not a solution for this question because the method is quite generic and doesn't known what is coming back.使用Read<T>不是这个问题的解决方案,因为该方法非常通用并且不知道返回什么。

If you don't mind aggregating the data first in a query, you can create a JOIN query and a matching Data Transfer Object class for Dapper to map to.如果您不介意先在查询中聚合数据,您可以创建一个 JOIN 查询和一个匹配的数据传输对象类,供 Dapper 映射到。

So say you have these tables:所以说你有这些表:

Orders table:

orders.id
orders.customerid
orders.date
OrderDetails table:

orderdetails.id
orderdetails.orderid
orderdetails.productid
orderdetails.quantity
orderdetails.cost

You can then create a joining query to return related data like this:然后,您可以创建一个连接查询来返回相关数据,如下所示:

SELECT 
  orders.id AS OrderId,
  orders.customer AS CustomerId,
  orders.date AS OrderDate,
  orderdetails.productid AS ProductId,
  orderdetails.quantity AS Quantity,
  orderdetails.cost AS Cost
FROM orders
LEFT JOIN orderdetails ON orders.id = orderdetails.orderid

And a matching DTO like this:和这样一个匹配的 DTO:

public class CustomerOrderDto : BaseDto
{
    public int OrderId { get; set; }
    public int CustomerId { get; set; }
    public DateTime OrderDate { get; set; }
    public int ProductId { get; set; }
    public double Quantity { get; set; }
    public double Cost { get; set; }
}

CustomerOrder retrieval methods:客户订单检索方法:

// get single order and its details
public CustomerOrder GetCustomerOrder(int id)
{
    string sql = @"SELECT 
                     orders.id AS OrderId,
                     orders.customer AS CustomerId,
                     orders.date AS OrderDate,
                     orderdetails.productid AS ProductId,
                     orderdetails.quantity AS Quantity,
                     orderdetails.cost AS Cost
                   FROM orders
                   LEFT JOIN orderdetails ON orderdetails.orderid = orders.id
                   WHERE orders.id = @Id";

    using (connection...)
    {
        return connection.Query<CustomerOrder>(sql, new { Id = id }).SingleOrDefault();
    }
}


// get all orders and their details
public IEnumerable<CustomerOrder> GetCustomerOrders()
{
    string sql = @"SELECT 
                     orders.id AS OrderId,
                     orders.customer AS CustomerId,
                     orders.date AS OrderDate,
                     orderdetails.productid AS ProductId,
                     orderdetails.quantity AS Quantity,
                     orderdetails.cost AS Cost
                   FROM orders
                   LEFT JOIN orderdetails ON orderdetails.orderid = orders.id";

    using (connection...)
    {
        return connection.Query<CustomerOrder>(sql);
    }
}

In our applications we'll actually have a static SELECT body string and create the sql WHERE clauses dynamically so that we don't have to repeat a bunch of code.在我们的应用程序中,我们实际上有一个静态的SELECT主体字符串并动态创建 sql WHERE子句,这样我们就不必重复一堆代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM