2

モデルがあるとしましょう

public Foo 
{
    string Something {get;set;}
    string SomethingElse {get;set;}
    string AnotherThing {get;set;}
}

これらのフィールドのいずれかに からの文字列が含まれているかどうかを判断する最も簡潔な方法は何Listですか?

var foo = new Foo 
{
    Something = "If Liverpool don't beat Fulham this evening I will cry",
    SomethingElse = "I hope I have that TekPub membership for Xmas",
    AnotherThing = "I wish NCrunch was a bit cheaper"
};

var keywords = new List<string> { "Liverpool", "glosrob", "stackoverflow" };

foo.Something「リバプール」という単語を含めて渡します。

4

2 に答える 2

3
var entriesSet = new HashSet<string>(foo.Something.Split());
entriesSet.UnionWith(foo.SomethingElse.Split());
entriesSet.UnionWith(foo.AnotherThing.Split());

if (entriesSet.Overlaps(keywords)) {
    ...
}
于 2012-12-22T16:12:36.217 に答える
2

何かのようなもの

new[] { foo.Something, foo.SomethingElse, foo.AnotherThing }.Any(s => keywords.Any(w => s.Contains(w)))
于 2012-12-22T16:12:30.413 に答える