2

假设我们有一个模型

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

确定这些字段中的任何一个是否包含来自 a 的任何字符串的最简洁方法是什么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 回答