Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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# StartsWith的扩展,用于搜索所有单词_C#_Linq_Search_Autosuggest_Startswith - Fatal编程技术网

C# StartsWith的扩展,用于搜索所有单词

C# StartsWith的扩展,用于搜索所有单词,c#,linq,search,autosuggest,startswith,C#,Linq,Search,Autosuggest,Startswith,字符串的starts是否有一个扩展,它会在字符串中每个单词的开头进行搜索 比如: “Ben Stiller”。启动扩展(“Sti”)返回true 我想要这个,这样我就可以做一个谓词来搜索 假设有一个名为Persons的列表,ICollection 每个人都有一个财产名称,其值为“Ben Stiller”或“Adam Sandler” 我希望能够做如下谓词: Persons.Where(p=>p.Name.StartsWithExtension(query)) 谢谢 (欢迎使用其他更好的方法)您可

字符串的starts是否有一个扩展,它会在字符串中每个单词的开头进行搜索

比如:
“Ben Stiller”。启动扩展(“Sti”)
返回true

我想要这个,这样我就可以做一个谓词来搜索

假设有一个名为Persons的列表,
ICollection

每个人都有一个财产名称,其值为“Ben Stiller”或“Adam Sandler”

我希望能够做如下谓词:

Persons.Where(p=>p.Name.StartsWithExtension(query))

谢谢
(欢迎使用其他更好的方法)

您可以先将字符串按单词拆分,如下所示:

var result = "Ben Stiller".Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                          .Any(x => x.StartsWith("Sti"));
public static bool AnyWordStartsWith(this string input, string test)
{
    return input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                .Any(x => x.StartsWith(test));
}
当然,您可以将其作为自己的扩展方法编写,如下所示:

var result = "Ben Stiller".Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                          .Any(x => x.StartsWith("Sti"));
public static bool AnyWordStartsWith(this string input, string test)
{
    return input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                .Any(x => x.StartsWith(test));
}
为什么不创建一个“ToWords”方法,然后将结果提供给StartsWith

事实上,“ToWords”已经存在了:

编辑:对于傻笑,让我们让它在倍数上工作

 var someNames = new []{ "Sterling Archer", "Cyril Figgus" };
 var q = someNames
    .Select(name => name.Split(' '))
    .SelectMany( word => word)
     // the above chops into words
    .Where(word => word.StartsWith("Arch"));

你甚至可以这样检查:

bool flag = (sample_str.StartsWith("Sti" ) || sample_str.Contains(".Sti") || sample_str.Contains(" Sti")) 

最简洁的方法可能是使用正则表达式:

public static bool StartsWithExtension(this string value, string toFind)
{
    return Regex.IsMatch(value, @"(^|\s)" + Regex.Escape(toFind));
}

这也比拆分字符“”上的源字符串更可靠,因为它可以处理其他空白字符。

如果姓氏有自己的含义(在本例中,您希望按其搜索/筛选),则可以将名称拆分为两个属性(名和姓)(可能是便于打印/记录的全名,或者你对person对象所做的任何事情)嗯,最简单的(即使不是“最佳”)方法可能是
Contains(“+str”);
if(sample_str.StartsWith(“Sti”)| sample_str.Contains(.Sti”)| sample_str Contains(.Sti”)| sample|str.Contains(“Sti”))return true;
是否需要将其用于Linq to Sql或Linq to Entities?@p.s.w.g无论哪种方式,这都不会起作用:该方法无法转换为Sql。它只在Linq to对象中起作用。此外,由于bcl正则表达式中固有的伪cachines,您可能会看到重复查询(swag)的性能提升
    public static bool ContainsWordStartingWith(this string aString, string startingWith)
    {
        return aString.Split(' ').Any(w => w.StartsWith(startingWith));
    }