[英]What is the best way to search for a string in a list of list of strings c#
What is the best way to search for a string in a list of lists of strings? 在字符串列表中搜索字符串的最佳方法是什么? Example I have many lists of strings eg List1 List 2 etc. and all of the lists are collected in a List of Lists (eg ListofLists<,...>. Now I want to search for the string "foo" in the list of lists. Is there a way to optimize this in c#?
示例我有很多字符串列表,例如List1 List 2等,并且所有列表都收集在列表列表中(例如ListofLists <,...>。现在我要在列表列表中搜索字符串“ foo”有没有办法在C#中对此进行优化?
Thanks in advance 提前致谢
Using Linq: 使用Linq:
To check if any of the lists contains the string 'foo': 要检查任何列表中是否包含字符串“ foo”:
collection.Any(x => x.Contains("foo"));
To get the particular list which contains the string 'foo': 要获取包含字符串“ foo”的特定列表,请执行以下操作:
collection.FirstOrDefault(x => x.Contains("foo"));
Does this work? 这样行吗?
var listOfLists = new List<List<string>>();
// insert code to populate listOfLists
var containsFoo = listOfLists.SelectMany(x => x).Any(x => x == "foo");
I'll just summarize/optimize other answers: 我将总结/优化其他答案:
The best way to do this is using LINQ. 最好的方法是使用LINQ。 Therefore you could use two ways:
因此,您可以使用两种方式:
Flaten the list 整理清单
This is done with SelectMany
: 这是通过
SelectMany
完成的:
listOfLists.SelectMany(x => x).Contains("foo");
SelectMany
combines all elements of the sublists and sublists of the sublists (and so on) into one list, so you can check all items, wether one contains the string ( https://msdn.microsoft.com/de-de/library/bb534336(v=vs.110).aspx ). SelectMany
将子列表的所有元素和子列表的子列表(等等)组合到一个列表中,因此您可以检查所有项目,其中一个包含字符串( https://msdn.microsoft.com/de-de/library/ bb534336(v = vs.110).aspx )。
Check all lists 查看所有清单
This is done with Any
: 这是通过
Any
完成的:
listOfLists.Any(x => x.Contains("foo"));
Any
checks wether any item fulfills the condition ( https://msdn.microsoft.com/de-de/library/bb337697(v=vs.110).aspx ). Any
检查是否满足任何条件( https://msdn.microsoft.com/de-de/library/bb337697(v=vs.110).aspx )。
Actually it seems to be more efficient to check all lists (With a randomly generated list with a total of 10,000 entries the first possibility needs averragely 34ms and the second one 31). 实际上,检查所有列表似乎更有效(对于随机生成的列表,该列表总共有10,000个条目,第一种可能性平均需要34毫秒,第二种可能性平均需要31毫秒)。
In both possibilities I use Contains
, which simply checks, wether the list contains the element ( https://msdn.microsoft.com/de-de/library/bb352880(v=vs.110).aspx ). 在这两种可能性中,我都使用
Contains
,它只检查列表是否包含元素( https://msdn.microsoft.com/de-de/library/bb352880(v=vs.110).aspx )。
Of course you could still use a loop as well: 当然,您仍然可以使用循环:
var contains = false;
foreach (var l in listOfLists)
foreach (var i in l)
if (i == "foo")
{
contains = true;
goto end;
}
end:
But this is less readable, less effective, more complicated and less elegant. 但这不那么易读,不太有效,更复杂且不太美观。
However, if you don't just want to check, wether it exists, but do a bit more with it, the last possibility is possibly the easiest one. 但是,如果您不只是要检查它是否存在,还可以对其进行更多操作,那么最后一种可能就是最简单的方法。 If you want an optimized version for another case, feel free to specify your requirements.
如果您想要针对其他情况的优化版本,请随时指定您的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.