0

我正在为客户开发一些工具,他们不想使用 LINQ to SQL,他们也不想使用 WPF 但我正在使用它,他们害怕新的编码或其他东西......但是我不能使用数据库,因为他们对 LINQ 一无所知......所以他们给了我这个烦人的课程(http://pastebin.com/VUzvN44i太长,无法粘贴在这里,抱歉)这是“好的”当逐行检索...

但是我有以下功能,它与 linq to sql 完全兼容,但是我迷失了他们给我的这个类......所以如果有人可以给我一些关于如何在不修改类的情况下做到这一点的建议,它将是很有帮助。谢谢。

private void GenerarFichero_Click(object sender, RoutedEventArgs e)
{
    string valEmisora = "02827003";
    string codigoCabecera;
    if (DateTime.Today.Day > 7)
        codigoCabecera = "AE570200";
    else
        codigoCabecera = "AE570100";
    DataBaseDataContext dc = new DataBaseDataContext();
    using (StreamWriter sw = new StreamWriter(codigoCabecera.Remove(6, 2)))
    {
        sw.WriteLine("775701    " + DateTime.Now.ToString("yy") + DateTime.Now.Month.ToString("d2") + DateTime.Now.AddDays(1).Day.ToString("d2") + DateTime.Now.Hour.ToString("d2") + DateTime.Now.Minute.ToString("d2") + "008910                    00" + txtBanco.Text + codigoCabecera + txtBanco.Text + "");
        sw.WriteLine("0170      " + valEmisora + "    " + this.txtBanco.Text + "          10" + DateTime.Now.ToString("MM") + DateTime.Now.ToString("yy"));

        var OutputQuery =
            from o in dc.Seguros
            group o by o.emisora;
        List<int> TotalRegistros = new List<int>();
        List<int> TotalSumas = new List<int>();
        foreach (var grupo in OutputQuery)
        {
            sw.WriteLine("0270      " + valEmisora + grupo.Key + " " + this.txtBanco.Text + "          10" + DateTime.Now.ToString("MM") + DateTime.Now.ToString("yy"));
            List<int> Suma = new List<int>();
            foreach (var data in grupo)
            {
                Suma.Add(Convert.ToInt32(data.importe + data.importe_dec));
                sw.WriteLine("6070      " + valEmisora + data.emisora + "1" + data.banco + data.sucursal + data.fecha + data.importe + data.importe_dec + data.identificacion + "                      " + data.referencia);
            }
            TotalRegistros.Add((grupo.Count() + 2));
            TotalSumas.Add(Suma.Sum());
            sw.WriteLine("8070      " + valEmisora + grupo.Key + " " + (grupo.Count() + 2).ToString().PadLeft(6, '0') + "        " + Suma.Sum().ToString().PadLeft(12, '0'));
        }
        sw.WriteLine("9070      " + valEmisora + "    " + (TotalRegistros.Sum() + 2).ToString().PadLeft(6, '0') + "        " + TotalSumas.Sum().ToString().PadLeft(12, '0'));
        this.txtTotal.Text = TotalSumas.Sum().ToString();
    }
    MessageBox.Show("El fichero ha sido guardado, ya no se puede editar");
}
4

2 回答 2

1

我对我要说的内容并不感到自豪,但既然你迫切需要一个解决方案,那就去吧......解决这个问题的快速而肮脏的方法是:

var connection = InitalizeConnectionToDb();
var select = new SelectBD();
// Get the data from database
select.Open(connection, @"select * from Seguros");
// Simulate the grouping
var dictionary = new Dictionary<string, List<DataRow>>();
foreach(var row in select.DataTable)
{
    var key = Convert.ToString(row["emisora"]);
    if(!dictionary.ContainsKey(key))
    {
        dictionary[key] = new List<DataRow>();
    }
    dictionary[key].Add(row);
}

现在您可以使用上面的字典来执行您的计算,因为数据是按emisora字段分组的。

于 2013-03-11T12:36:31.400 回答
0

好吧,我有最终的代码工作,我已经替换OutputQueryDataTable.AsEnumerable实际上它正在工作......这就是代码现在的样子

private void GenerarFichero_Click(object sender, RoutedEventArgs e)
{
    string valEmisora = "02827003";
    string codigoCabecera;
    if (DateTime.Today.Day > 7)
        codigoCabecera = "AE570200";
    else
        codigoCabecera = "AE570100";
    using (StreamWriter sw = new StreamWriter(codigoCabecera.Remove(6, 2)))
    {
        sw.WriteLine("775701    " + DateTime.Now.ToString("yy") + DateTime.Now.Month.ToString("d2") + DateTime.Now.AddDays(1).Day.ToString("d2") + DateTime.Now.Hour.ToString("d2") + DateTime.Now.Minute.ToString("d2") + "008910                    00" + txtBanco.Text + codigoCabecera + txtBanco.Text + "");
        sw.WriteLine("0170      " + valEmisora + "    " + this.txtBanco.Text + "          10" + DateTime.Now.ToString("MM") + DateTime.Now.ToString("yy"));

        SelectBD sel = new SelectBD(App.ConexBD, "SELECT * FROM Seguros");
        var Query = sel.DataTable.AsEnumerable().Select(row =>
                {
                    return new      
                    {      
                        banco = row["banco"].ToString(),
                        emisora = row["emisora"].ToString(),
                        sucursal = row["sucursal"].ToString(),
                        fecha = row["fecha"].ToString(),
                        identificacion = row["identificacion"].ToString(),
                        importe = row["importe"].ToString(),
                        importe_dec = row["importe_dec"].ToString(),
                        provincia = row["provincia"].ToString(),
                        referencia = row["referencia"].ToString(),
                    };
                });
        var OutputQuery = Query.GroupBy(l => l.emisora);
        List<int> TotalRegistros = new List<int>();
        List<int> TotalSumas = new List<int>();
        foreach (var grupo in OutputQuery)
        {
            sw.WriteLine("0270      " + valEmisora + grupo.Key + " " + this.txtBanco.Text + "          10" + DateTime.Now.ToString("MM") + DateTime.Now.ToString("yy"));
            List<int> Suma = new List<int>();
            foreach (var data in grupo)
            {
                Suma.Add(Convert.ToInt32(data.importe + data.importe_dec));
                sw.WriteLine("6070      " + valEmisora + data.emisora + "1" + data.banco + data.sucursal + data.fecha + data.importe + data.importe_dec + data.identificacion + "                      " + data.referencia);
            }
            TotalRegistros.Add((grupo.Count() + 2));
            TotalSumas.Add(Suma.Sum());
            sw.WriteLine("8070      " + valEmisora + grupo.Key + " " + (grupo.Count() + 2).ToString().PadLeft(6, '0') + "        " + Suma.Sum().ToString().PadLeft(12, '0'));
        }
        sw.WriteLine("9070      " + valEmisora + "    " + (TotalRegistros.Sum() + 2).ToString().PadLeft(6, '0') + "        " + TotalSumas.Sum().ToString().PadLeft(12, '0'));
        this.txtTotal.Text = TotalSumas.Sum().ToString();
    }
    MessageBox.Show("El fichero ha sido guardado, ya no se puede editar");
}
于 2013-03-11T12:36:11.443 回答