-1

I am in the process of generating XML from a CSV file.

In the picture I attach the result I am waiting for

enter image description here

using (var reader = new StreamReader(csvPath + csvName))
{
    using (var csv = new CsvReader(reader))
    {

        csv.Configuration.HasHeaderRecord = true;
        csv.Configuration.RegisterClassMap<MapClass>();
        var records = csv.GetRecords<Data>().ToList();

         from w in records
         select

         // group by invoice number ?

        new XElement("INVOICE",
            new XAttribute("DATE",w._invoiceDate ),
            new XAttribute("NUMBER", w._invoiceNumber),
            new XAttribute("QTY", w._orderQty)  

        }
}

I want to display the data grouped by the invoice number with the amounts added up (equivalent to a pivot table). Can I ask you for a tip for me?

using (var reader = new StreamReader(csvPath + csvName))
            {
                using (var csv = new CsvReader(reader))
                {

                    csv.Configuration.HasHeaderRecord = true;
                    csv.Configuration.RegisterClassMap<MapClass>();
                    List<Data> records = csv.GetRecords<Data>().ToList();


                    var result = records.
                        GroupBy(x => x._invoiceNumber).
                        Select(x => new
                        {
                            // IGrouping does't contain 'Value" definition
                            InvoiceNumber = x.Key,
                            InvoiceQty = x.Value.Sum(y => y._orderQty),
                            InvoiceTotal = x.Value.Sum(y => y._invoiceTotal)
                        }).ToList();
                }

            }
enginne
  • 1
  • 1

2 Answers2

0

below code should give to data of table you expect, further you can generate XML you need

var result = records.GroupBy(x => x._invoiceNumber).Select(x => new { InvoiceNumber = x.Key, InvoiceQty = x.Sum(y => y._orderQty), InvoiceTotal = x.Sum(y => y._invoiceTotal) }).ToList()
Arjun Vachhani
  • 1,761
  • 3
  • 21
  • 44
0

We can close this topic. I found a solution

var res =   from e in records
                        group e by e._invoiceNumber into g
                        where g.Count() >= 1
                        select new
                        {
                            Invoice = g.Key,
                            Total = g.Count(),
                            TotalQty = g.Sum(x => Convert.ToInt32( x._orderQty.ToString()))
                        };


                        foreach(var item in res)
                        {
                            Console.WriteLine($" {item.Invoice}  :   {item.TotalQty}  ");
                        }
enginne
  • 1
  • 1