让我的脚湿润Linq。我试图确定四个DataColumns中包含的不同值。所以,我从访问空Linq结果非常缓慢
var c1types = (from DataRow row in dtSource.Select("hasreq")
where row["m"].ToInt() > 0
select new { col = row["m"] }).Distinct();
var c2types = (from DataRow row in dtSource.Select("hasreq")
where row["w"].ToInt() > 0
select new { col = row["w"] }).Distinct();
var c3types = (from DataRow row in dtSource.Select("hasreq")
where row["ag"].ToInt() > 0
select new { col = row["ag"] }).Distinct();
var c4types = (from DataRow row in dtSource.Select("hasreq")
where row["aq"].ToInt() > 0
select new { col = row["aq"] }).Distinct();
foreach (var type in c1types.Union(c2types).Union(c3types).Union(c4types).Distinct())
{
...
}
这个工程,但是很慢(4-5秒)。于是,我把在foreach之前以下
MessageBox.Show(c1types.Count().ToString()); // 1 - immediate display
MessageBox.Show(c2types.Count().ToString()); // 1 - immediate display
MessageBox.Show(c3types.Count().ToString()); // 1 - immediate display
MessageBox.Show(c4types.Count().ToString()); // 0 - 4-5 seconds to display
用我的样本数据中,每个前三个选择的返回单个独特的值(计数()== 1)。第四个返回没有值(Count()== 0)。我不明白的是为什么它瞬间显示前三个计数,但第四个显示4-5秒。看起来空洞的结果是经济放缓的原因。这里发生了什么,什么是最好的解决方法?
你使用.net 3.5或.net 4.0吗? – Luke101 2010-06-11 00:22:40
我正在使用3.5。 – PahJoker 2010-06-11 00:53:35
我没有太多的DataTable经验 - 是每个查询数据库后端的查询,还是只查询内存中的数据?如果一个数据库,检查sql(sql分析器,linqpad,无论什么)可能有助于弄清楚发生了什么(缺少索引?坏查询?等) – 2010-06-11 06:34:46