Search code examples
c#linqhttppostedfilebase

Linq over InputStream from HttpPostedFileWrapper


Is it possible to apply a Linq query from a HttpPostedFileWrapper?

My web app allows users to select a bunch of .csv files. I now need to open those files and import them.

My previous code, which uses paths and file names looks like;

            importedList = (from csvLine in File.ReadAllLines(fileName)
                            let x = csvLine.Split(',')
                            select new ImportedXDock
                            {
                                StoreNumber = int.Parse(x[0]),
                                DCNumber = int.Parse(x[1]),
                                DeliveryDay = x[2],
                                Activity = x[3],
                                ActivityDay = x[4],
                                Time = TimeSpan.Parse(x[5])

                            }).ToList();

However, now that i have a collection of HttpPostedFileWrapper objects how would I do the same?

edit

Or do I need to convert it to something and then read the file?


Solution

  • You may be able to loop over the file names instead of the input streams

    foreach (var fileName in wrapper.Select(w => w.FileName))
    {
        yield return (from csvLine in File.ReadAllLines(fileName)
                        let x = csvLine.Split(',')
                        select new ImportedXDock
                        {
                            StoreNumber = int.Parse(x[0]),
                            DCNumber = int.Parse(x[1]),
                            DeliveryDay = x[2],
                            Activity = x[3],
                            ActivityDay = x[4],
                            Time = TimeSpan.Parse(x[5])
    
                        }).ToList();
    }