Question

I am trying to create an application that will extract some data out of an automatically generated excel file. This can be very easily done with Access but the file is in Excel and the solution must be a one button sort of thing.

For some reason, simply looping through the data without doing any actions is slow. The code below is my attempt at optimizing it from something that was far slower. I have arrived at using Linq to SQL after a few attempts at this with the Interop classes directly and through different wrappers.

I also have read the answers to a few questions on here and Google. In an attempt to see what is causing the slowness, I have removed all instructions but kept "i++" from the relevant section. It is still very slow. I also tried to optimize it by limiting the number of records retrieved in the where clause in the third line but that didn't work. Your help would be appreciated.

Thank you.

        Dictionary<string,double> instructors = new Dictionary<string,double>();
        var t = from c in excel.Worksheet("Course_201410_M1")
               // where c["COURSE CODE"].ToString().Substring(0,4) == "COSC" || c["COURSE CODE"].ToString().Substring(0,3) == "COEN" || c["COURSE CODE"].ToString().Substring(0,3) == "GEIT" || c["COURSE CODE"].ToString().Substring(0,3) == "ITAP" || c["COURSE CODE"] == "PRPL 0012" || c["COURSE CODE"] == "ASSE 4311" || c["COURSE CODE"] == "GEEN 2312" || c["COURSE CODE"] == "ITLB 1311"
                select c;
        HashSet<string> uniqueForce = new HashSet<string>();
        foreach (var c in t)
        {
            if(uniqueForce.Add(c["Instructor"]))
                instructors.Add(c["Instructor"],0.0);
        }
        foreach (string name in instructors.Keys)
        {
            var y = from d in t
                    where d["Instructor"] == name
                    select d;
            int i = 1;
            foreach(var z in y)
            {
                //this is the really slow. It takes a couple of minutes to finish. The 
                // file has less than a 1000 records.
                i++;
            }


        }
Était-ce utile?

La solution

Put the query that forms var t into brackets and then call ToList() on it.

     var t = (from c in excel.Worksheet("Course_201410_M1")
     select c).ToList();

Due to linq's lazy/deferred execution model, whenever you iterate over the collection it will requery the data source unless you give it a List to work with.

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