1

文字列のリストがあります

string[] arr = new string[] { "hello world", "how are you", "what is going on" };

私が与えた文字列が、arr

だから私が持っているとしましょう

string s = "hello are going on";

のすべての単語sが の文字列の 1 つにあるため、一致します。arr

string s = "hello world man"

"man" がどの文字列にも含まれていないため、これは一致しません。arr

これを行うための「より長い」メソッドを作成する方法は知っていますが、作成できる素敵な linq クエリはありますか?

4

3 に答える 3

3
string[] arr = new string[] { "hello world", "how are you", "what is going on" };
string s = "hello are going on";
string s2 = "hello world man";
bool bs = s.Split(' ').All(word => arr.Any(sentence => sentence.Contains(word)));
bool bs2 = s2.Split(' ').All(word => arr.Any(sentence => sentence.Contains(word)));
于 2013-03-13T19:03:25.557 に答える
2
        string[] arr = new string[] { "hello world", "how are you", "what is going on" };

        HashSet<string> incuded = new HashSet<string>(arr.SelectMany(ss => ss.Split(' ')));

        string s = "hello are going on";
        string s2 = "hello world man";

        bool valid1 = s.Split(' ').All(ss => incuded.Contains(ss));
        bool valid2 = s2.Split(' ').All(ss => incuded.Contains(ss));

楽しみ!(私はパフォーマンスにハッシュセットを使用しました。すべての場合で、「incuded」(愚かなタイプミス)をarr.SelectMany(ss => ss.Split(''))。Unique()に置き換えることができます。

于 2013-03-13T19:02:25.573 に答える
0

I try my best to one-line it :)

var arr = new [] { "hello world", "how are you", "what is going on" };

var check = new Func<string, string[], bool>((ss, ar) => 
    ss.Split(' ').All(y => ar.SelectMany(x => 
        x.Split(' ')).Contains(y)));

var isValid1 = check("hello are going on", arr);
var isValid2 = check("hello world man", arr);
于 2013-03-13T19:13:05.343 に答える