Question

I am trying to use LinqToExcel to solve an issue, and I am having some problems. We get this excel file that tells us the product that we will be receiving. I need to read that file and save the updates to the database. Everything is fine except when we get 2 records in the file with the same partNumber. The requirement is to merge the records prior to saving them to the database. If I could use "group by" I could just sum the Qty, but apparently I can't do a group by with LinkToExcel, so can anyone give me some hints on how to solve this issue?

var product = from x in book.Worksheet("FormularyImport")
    group by x.InternalPN into g_pn
    select g_pn.First().InternalPN, g_pn.First().Name, g_pn.Sum(x => x.Qty);

foreach (string product in product)
{
   //Here I want to iterate through each product, and persist the imported data to the database.
}
Était-ce utile?

La solution

If you convert it to an in memory list first, then you can use group by.

var product = from x in book.Worksheet("FormularyImport").ToList()
    group by x.InternalPN into g_pn
    select g_pn.First().InternalPN, g_pn.First().Name, g_pn.Sum(x => x.Qty);

foreach (string product in product)
{
   //Here I want to iterate through each product, and persist the imported data to the database.
}

Notice the ToList() call after Worksheet("FormularyImport")

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top