我有一个包含超过75,000个对象的列表.要从列表中搜索项目,我正在使用以下代码.
from nd in this.m_ListNodes
where
nd.Label == SearchValue.ToString()
select
nd;
Run Code Online (Sandbox Code Playgroud)
这段代码有效吗?
Jon*_*eet 11
您需要多久搜索一次相同的列表?如果您只搜索一次,您也可以进行直线搜索 - 尽管您可以通过在查询之前调用SearchValue.ToString() 一次来使当前代码稍微提高效率.
如果您要多次在同一列表上执行此搜索,则应该构建一个Lookup或一个Dictionary:
var lookup = m_ListNodes.ToLookup(nd => nd.Label);
Run Code Online (Sandbox Code Playgroud)
要么
var dictionary = m_ListNodes.ToDictionary(nd => nd.Label);
Run Code Online (Sandbox Code Playgroud)
如果每个标签只有一个条目,请使用字典; 如果可能有多个匹配项,请使用查找.
要使用这些,查找:
var results = lookup[SearchValue.ToString()];
// results will now contain all the matching results
Run Code Online (Sandbox Code Playgroud)
或者字典:
WhateverType result;
if (dictionary.TryGetValue(SearchValue.ToString(), out result))
{
// Result found, stored in the result variable
}
else
{
// No such item
}
Run Code Online (Sandbox Code Playgroud)