确定三个字段中的一个是否具有关键字列表中的任何一个
本文关键字:关键字 列表 任何一 是否 字段 三个 一个 | 更新日期: 2023-09-27 17:49:21
假设我们有一个模型
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" };
将传递包含单词'Liverpool'的foo.Something
。
var entriesSet = new HashSet<string>(foo.Something.Split());
entriesSet.UnionWith(foo.SomethingElse.Split());
entriesSet.UnionWith(foo.AnotherThing.Split());
if (entriesSet.Overlaps(keywords)) {
...
}
类似
new[] { foo.Something, foo.SomethingElse, foo.AnotherThing }.Any(s => keywords.Any(w => s.Contains(w)))