Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用LINQ在字符串列表中查找子字符串_C#_Linq - Fatal编程技术网

C# 如何使用LINQ在字符串列表中查找子字符串

C# 如何使用LINQ在字符串列表中查找子字符串,c#,linq,C#,Linq,我需要使用它的子字符串从字符串集合中找出一个字符串。这个子字符串 一定是在起步阶段 你可以这样做 collection.FirstOrDefault(s => s.StartsWith(whatever)) List<string> collection = new List<string>(); collection.Add("example sample"); collection.Add("sample");

我需要使用它的子字符串从字符串集合中找出一个字符串。这个子字符串
一定是在起步阶段

你可以这样做

collection.FirstOrDefault(s => s.StartsWith(whatever))
List<string> collection = new List<string>();
        collection.Add("example sample");
        collection.Add("sample");
        collection.Add("example");

        var varSubstring = collection.Where(x => x.IndexOf("sample")==0).ToList();
        foreach (var vartemp in varSubstring)
        {
        }
列表集合=新列表();
收集。添加(“示例样本”);
收集。添加(“样本”);
集合。添加(“示例”);
var varSubstring=collection.Where(x=>x.IndexOf(“sample”)==0.ToList();
foreach(varSubstring中的vartemp变量)
{
}

您可以这样做

List<string> collection = new List<string>();
        collection.Add("example sample");
        collection.Add("sample");
        collection.Add("example");

        var varSubstring = collection.Where(x => x.IndexOf("sample")==0).ToList();
        foreach (var vartemp in varSubstring)
        {
        }
列表集合=新列表();
收集。添加(“示例样本”);
收集。添加(“样本”);
集合。添加(“示例”);
var varSubstring=collection.Where(x=>x.IndexOf(“sample”)==0.ToList();
foreach(varSubstring中的vartemp变量)
{
}

或者,如果可能有多个,则使用
collection。其中(s=>s.StartsWith(无论什么))
或者,如果可能有多个,则使用
collection。其中(s=>s.StartsWith(无论什么))
尝试这个和这个