简体   繁体   English

在字符串列表C#中搜索字符串的最佳方法是什么

[英]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: 因此,您可以使用两种方式:

  1. You could flaten the list and look wether it contains an element 您可以展平列表,并查看其中是否包含元素
  2. You could check all lists wether one list contains the element 您可以检查所有列表,而其中一个列表包含元素

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.

相关问题 转换列表的最佳方法是什么<string>列出<type>在 C# 中 - What is the best way to convert List <string> to List<type> in C# 搜索List的最佳方法是什么 <List<string> &gt;? - What is the best way to search a List<List<string>>? 在 C# 中的 100 万个字符串列表中搜索字符串的更好方法 - Better way to search string in list of 1 million strings in C# 在C#中,查看列表是否包含其他列表的最佳方法是什么? - In C#, What is the best way to see if a list contains another list? 在C#中,由班级成员之一搜索班级列表的最佳方法是什么? - In C#, what's the best way to search a list of a class by one of the class's members? 通过查看字符串列表来获取与字符串中的字符串匹配的字符串列表的最佳方法是什么? - What's the best way to acquire a list of strings that match a string inside a string, by looking through a string list? 在C#中,按字符串属性对对象列表进行排序并获得正确顺序的最佳方法是什么? - In C#, what is the best way to sort a list of objects by a string property and get correct order? 在C#中,转换List的最佳方法是什么 <T> 到SortedDictionary <string, T> ? - In C#, what is the best way to convert a List<T> to a SortedDictionary<string, T>? 在C#中制作星期四的下拉列表的最佳方法是什么? - What is the best way to make a DropDown list of Thursdays in C#? 在C#中比较复杂对象的两个列表的最佳方法是什么 - What is the best way to compare two list of complex object in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM