繁体   English   中英

极慢的Linq到Excel

[英]Extremely Slow Linq to Excel

我正在尝试创建一个应用程序,该应用程序将从自动生成的Excel文件中提取一些数据。 使用Access可以很容易地做到这一点,但是文件在Excel中,解决方案必须是一键式的事情。

由于某些原因,简单地循环访问数据而不执行任何操作很慢。 下面的代码是我尝试从慢得多的东西进行优化。 在直接和通过不同的包装器与Interop类进行几次尝试之后,我开始使用Linq to SQL。

我还阅读了有关此处和Google的一些问题的答案。 为了查看导致速度慢的原因,我删除了所有指令,但在相关部分中保留了“ i ++”。 它仍然很慢。 我还尝试通过限制第三行的where子句中检索到的记录数来对其进行优化,但这是行不通的。 您的帮助将不胜感激。

谢谢。

        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++;
            }


        }

将构成var t的查询放在方括号中,然后在其上调用ToList()。

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

由于linq的延迟/延迟执行模型,每当您对集合进行迭代时,除非您为其提供要使用的List,否则它将重新查询数据源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM