日期:2014-05-20  浏览次数:21452 次

linq如何提高查询效率?

ItemProp是一个类;itemProps 有几百万条数据,现在这样查,一秒钟大概只能构造一百条itemProp ,请问要怎么提高效率呢?
 
   List<ItemProp> itemProps = this.GetItemProps(dir);
   List<ItemProp> newProps = new List<ItemProp>();
   DataTable dtPid = DB.GetDataSet("select AttributeId,CategoryId from erp.dbo.Category2Attributes", DB.DataBase.ERPDBSlave).Tables[0];
               foreach (DataRow row in dtPid.Rows)
                {
                    long pid = long.Parse(row["AttributeId"].ToString());
                    long cid = long.Parse(row["CategoryId"].ToString());
                    ItemProp itemProp = (from a in itemProps
                                         where a.Cid == cid && a.Pid == pid
                                         select a).FirstOrDefault();
                    newProps.Add(itemProp);
                }
                newProps = itemProps.Except(newProps).ToList();
linq?查询

------解决方案--------------------
用HASHSET。

1: 先把数据库里所有的东东弄出来,SELECT ID ONLY, 然后放入HASHSET。(注意别用.ToList()),用ienumerable)