【发布时间】:2016-04-08 12:48:57
【问题描述】:
我有一个包含一些项目的巨大列表,我需要搜索我需要的任何内容是否已添加到列表中,是否要在文本行中显示结果,并且在搜索已满或没有匹配项之后,删除“正在搜索...”行。 到目前为止,我有这段代码,如果有匹配或没有找到,搜索不会消失:
Dictionary<string, Line> Smartphones = new Dictionary<string, Line>
{
{"Smartphones/Sony", new Line { Text = "Sony Xperia Z5 Premium", Color = () => Settings.Sony }},
{"Smartphones/iPhone", new Line { Text = "iPhone 6S Plus", Color = () => Settings.iPhone }},
{"Smartphones/Samsung", new Line { Text = "Galaxy S6 Edge", Color = () => Settings.Samsung }}
};
Line alert_me = Smartphones.Where(kv => text.StartsWith(kv.Key, StringComparison.OrdinalIgnoreCase)).Select(kv => kv.Value).FirstOrDefault();
if (alert_me != null)
{
if (alert.Contains(new Line { Text = "Searching...", Color = () => Settings.Smartphones }))
{
alert.Remove(new Line { Text = "Searching...", Color = () => Settings.Smartphones });
}
alert.Add(alert_me); return;
}
if (text.Contains("Samsung"))
{
alert.Add(new Line { Text = "Searching...", Color = () => Settings.Smartphones });
}
还有其他现代/优雅的方法可以做到这一点...我只是认为代码对于匹配搜索来说太大了。
谢谢,
【问题讨论】:
标签: c# string dictionary full-text-search