Dapper的參數擴展代碼不處理帶有列表的IN查詢,但是DapperExtensions謂詞系統確實允許IN查詢,並且如果該參數是列表類型,則會將等於查詢謂詞轉換爲IN查詢。
如果您想堅持使用Dapper進行映射,可以將Jonathan Willcock的sysobjects/sysindexes查詢作爲視圖進行擴展,並創建一個類來表示結果。然後,您可以組裝一個DapperExtensions的GetList查詢適當的參數
視圖創建:
CREATE VIEW TableRowCounts
AS
SELECT t.name TableName, i.rows Records
FROM sysobjects t INNER JOIN sysindexes i ON i.ID = t.ID where t.xtype = 'U' and i.indid in (0,1)
班結果:
public class TableRowCounts
{
public string TableName { get; set; }
public int Records { get; set; }
}
DapperExtensions查詢來滋潤效果:
PredicateGroup query = new PredicateGroup();
List<string> tables;
tables = new List<string> { "table1", "table2" };
query.Predicates = new List<IPredicate>();
query.Predicates.Add(Predicates.Field<TableRowCounts>(f => f.TableName, Operator.Eq, tables));
List<TableRowCounts> results;
results = _connection.GetList<TableRowCounts>(query).ToList();
作爲關於IN表演的備註,您可能會看到表演博士如果你的參數列表(在這個例子中是表名)大於大約200個條目,請選擇op。
您正在使用哪個數據庫? – 2016-08-18 08:38:09
使用SQL Server – Manjari