QueryWithRowMapperDelegate性能问题 - 替代解决方案

时间:2016-07-01 14:45:30

标签: c# ado.net

我使用QueryWithRowMapperDelegate返回结果集,并将映射列返回到类中的不同字段。下面的代码片段与我的代码非常相似。这可以很好地返回少量行,但是当我的ResultSet包含3400多行时,需要很长时间才能遍历它。这只是使用这种方法的限制吗?

任何替代解决方案?

List<Customer> customers = Ado.AdoTemplate.QueryWithRowMapperDelegate<Customer>(CommandType.Text, cmdText,
new RowMapperDelegate<Customer>((reader, rowNum) =>
{
   Customer customer = new Customer();
   customer.Address = reader.GetString(0, string.Empty);
   customer.City = reader.GetString(1, string.Empty);
   customer.CompanyName = reader.GetString(2, string.Empty);
   customer.ContactName = reader.GetString(3, string.Empty);
   customer.ContactTitle = reader.GetString(4, string.Empty);
   customer.Country = reader.GetString(5, string.Empty);
   customer.Fax = reader.GetString(6, string.Empty);
   customer.Id = reader.GetString(7, string.Empty);
   customer.Phone = reader.GetString(8, string.Empty);
   customer.PostalCode = reader.GetString(9, string.Empty);
   customer.Region = reader.GetString(10, string.Empty);
   return customer;
})).ToList();

1 个答案:

答案 0 :(得分:0)

ResultSetExtractorDelegate<Customer>是一个委托,因此它不是预编译的。编写具有相同功能的方法并将其传递给QueryWithRowMapperDelegate。 在您的Customer类中:

            public static Customer GetFromRecord(IDataReader reader, int rownum)
        {
            Customer customer = new Customer();
            customer.Address = reader.GetString(0, string.Empty);
            customer.City = reader.GetString(1, string.Empty);
            customer.CompanyName = reader.GetString(2, string.Empty);
            customer.ContactName = reader.GetString(3, string.Empty);
            customer.ContactTitle = reader.GetString(4, string.Empty);
            customer.Country = reader.GetString(5, string.Empty);
            customer.Fax = reader.GetString(6, string.Empty);
            customer.Id = reader.GetString(7, string.Empty);
            customer.Phone = reader.GetString(8, string.Empty);
            customer.PostalCode = reader.GetString(9, string.Empty);
            customer.Region = reader.GetString(10, string.Empty);
            return customer;
        }

List<Customer> customers = Ado.AdoTemplate.QueryWithRowMapperDelegate<Customer>(CommandType.Text, cmdText, Customer.GetFromRecord).ToList();

它应该在更短的时间内给出相同的结果。

此致 彼得

相关问题