linq如何提高查询效率?
ItemProp是一个类;itemProps 有几百万条数据,现在这样查,一秒钟大概只能构造一百条itemProp ,请问要怎么提高效率呢?
linq?查询
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();
[解决办法]
用HASHSET。
1: 先把数据库里所有的东东弄出来,SELECT ID ONLY, 然后放入HASHSET。(注意别用.ToList()),用ienumerable)
2:把你源文件里所有的东东读到ienumerable里,然后依然是SELECT ID ONLY,然后也放入HASHSET.
3: 比较这2个HASHSET.
用HASHSET的好处是,速度是最快的,而且只走一遍。
itemProps = this.GetItemProps(dir); //你这里要修改下,别返回LIST,应该返回ienumerable
var itemPropsHs = new HahSet<int>(itemProps.Select(x=>x.Id));
我盲打的哈,请多见谅。