DataTable.Select(查询)需要花费很多时间

时间:2013-07-09 01:19:21

标签: c# performance datatable compact-framework dataview

这是我的代码:

public string[] RegresaprecionombreyId(DataTable tablalm, DataTable tablaep, string producto, string cliente, DateTime fecha)
{
    producto = ((int)Convert.ToDecimal(producto)).ToString();
    string precio = "0.00", preciobase = "0.00", nombre = "producto no encontrado", id = "00000000-0000-0000-0000-000000000000";

    if (tablaep.Rows.Count > 0)
    {
        string consulta = "ope_pro=" + producto + " and cliente=" + cliente + " and '" + fecha.ToString("yyyy-MM-dd") + "' >= inicio and '" + fecha.ToString("yyyy-MM-dd") + "' <= fin";
        DataRow[] filas = tablaep.Select(consulta);

        if (filas.Length > 0)
        {
            id = filas[0]["id"].ToString();
            nombre = filas[0]["nombre"].ToString();
            precio = filas[0]["precio"].ToString();
            preciobase = filas[0]["preciobase"].ToString();
            return new string[] { precio, nombre, id, preciobase };
        }
    }

    if (tablalm.Rows.Count > 0)
    {
        string consulta = "ope_pro='" + producto + "'";
        DataRow[] filas = tablalm.Select(consulta);

        if (filas.Length > 0)
        {
            id = filas[0]["id"].ToString();
            nombre = filas[0]["nombre"].ToString();
            DateTime fechatabla = Convert.ToDateTime(filas[0]["fechanue"].ToString());

            if (fecha >= fechatabla)
                precio = filas[0]["precionue"].ToString();
            else
                precio = filas[0]["precio"].ToString();

            return new string[] { precio, nombre, id, precio };
        }
    }

    return new string[] { precio, nombre, id, preciobase };
}

我使用了一些DataTable.Select(查询)

使用此方法的其他代码是循环并使用DataTable.Select获取帐户的id,输入此方法并在2表再次使用DataTable.Select(query) 第一张表是每个帐户每个产品的价格不同的表格 例如

account  product  price
1          1      10
1          2      11
1          3      12
2          1      11
2          2      12
2          3      13

如果现在没有出现在这个DataTable中,我会在其他表格中找到这种格式

product  price
  1       10.5
  2       11.5
  3       11.5

第一个数据表有17569行;第二个数据表有597行。

那么当我打电话给这个方法时

RegresaprecionombreyId(DataTable tablalm, DataTable tablaep, string producto, string cliente, DateTime fecha)

这需要大约3或4秒,每次我调用它,我有一个大约3245条记录的循环,所以3245(记录)* 3(秒)这超过2小时。我怎样才能改善这段时间?

0 个答案:

没有答案
相关问题