Query

2022-04-21 09:42 更新

描述

Query是一個(gè)可以從IDbConnection類型的任意對(duì)象調(diào)用的擴(kuò)展方法,它可以執(zhí)行查詢并映射結(jié)果。

結(jié)果可以映射到:

  • 匿名類型
  • 強(qiáng)類型
  • 多映射(一對(duì)一)
  • 多映射(一對(duì)多)
  • 多類型

參數(shù)

下表顯示了Query方法的不同參數(shù)。

名稱 描述
sql 要執(zhí)行的查詢。
param 查詢參數(shù)(默認(rèn)為null)。
transaction 需要使用的事務(wù)(默認(rèn)為null)。
buffered 是否從緩沖讀取查詢結(jié)果(默認(rèn)為true)。
commandTimeout 命令執(zhí)行超時(shí)時(shí)間(默認(rèn)為null)。
commandType 命令類型(默認(rèn)為null)。

案例 - 查詢匿名類型

原生SQL查詢可以使用Query方法執(zhí)行,并將結(jié)果映射到動(dòng)態(tài)類型列表。

string sql = "SELECT * FROM Invoice;";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var invoices = connection.Query(sql).ToList();

    My.Result.Show(invoices);

    //輸出:3 anonymous entity returned
}

案例 - 查詢強(qiáng)類型

原生SQL查詢可以使用Query方法執(zhí)行,并將結(jié)果映射到強(qiáng)類型列表。

string sql = "SELECT * FROM Invoice;";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var invoices = connection.Query<Invoice>(sql).ToList();

    My.Result.Show(invoices);

    //輸出:3 invoice(s) returned
}

案例 - 查詢多映射(一對(duì)一)

原生SQL查詢可以使用Query方法執(zhí)行,并將結(jié)果映射到具有一對(duì)一關(guān)系的強(qiáng)類型列表。

string sql = "SELECT * FROM Invoice AS A INNER JOIN InvoiceDetail AS B ON A.InvoiceID = B.InvoiceID;";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var invoices = connection.Query<Invoice, InvoiceDetail, Invoice>(
            sql,
            (invoice, invoiceDetail) =>
            {
                invoice.InvoiceDetail = invoiceDetail;
                return invoice;
            },
            splitOn: "InvoiceID")
        .Distinct()
        .ToList();

    My.Result.Show(invoices);

    //輸出:3 invoice(s) returned (Including InvoiceDetail information) 
}

案例 - 查詢多映射(一對(duì)多)

原生SQL查詢可以使用Query方法執(zhí)行,并將結(jié)果映射到具有一對(duì)多關(guān)系的強(qiáng)類型列表。

string sql = "SELECT * FROM Invoice AS A INNER JOIN InvoiceItem AS B ON A.InvoiceID = B.InvoiceID;";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var invoiceDictionary = new Dictionary<int, Invoice>();

    var invoices = connection.Query<Invoice, InvoiceItem, Invoice>(
            sql,
            (invoice, invoiceItem) =>
            {
                Invoice invoiceEntry;

                if (!invoiceDictionary.TryGetValue(invoice.InvoiceID, out invoiceEntry))
                {
                    invoiceEntry = invoice;
                    invoiceEntry.Items = new List<InvoiceItem>();
                    invoiceDictionary.Add(invoiceEntry.InvoiceID, invoiceEntry);
                }

                invoiceEntry.Items.Add(invoiceItem);
                return invoiceEntry;
            },
            splitOn: "InvoiceID")
        .Distinct()
        .ToList();

    My.Result.Show(invoices);

    //輸出:3 invoice(s) returned (Including 6 InvoiceItem) 
}

案例 - 查詢多類型

原生SQL查詢可以使用Query方法執(zhí)行,并將結(jié)果映射到不同類型的列表。

string sql = "SELECT * FROM Invoice;";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var invoices = new List<Invoice>();

    using (var reader = connection.ExecuteReader(sql))
    {
        var storeInvoiceParser = reader.GetRowParser<StoreInvoice>();
        var webInvoiceParser = reader.GetRowParser<WebInvoice>();

        while (reader.Read())
        {
            Invoice invoice;

            switch ((InvoiceKind) reader.GetInt32(reader.GetOrdinal("Kind")))
            {
                case InvoiceKind.StoreInvoice:
                    invoice = storeInvoiceParser(reader);
                    break;
                case InvoiceKind.WebInvoice:
                    invoice = webInvoiceParser(reader);
                    break;
                default:
                    throw new Exception(ExceptionMessage.GeneralException);
            }

            invoices.Add(invoice);
        }
    }

    My.Result.Show(invoices); 

    //輸出:3 invoice(s) returned (StoreInvoice:1, WebInvoice:2) 
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)