2012-12-22 22 views
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包含單詞'利物浦'。

+1

請記住標記您的問題與相關語言(S)。我爲你解決了這個問題,但我希望2k +代表用戶知道如何在第一時間做到正確。 –

回答

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

if (entriesSet.Overlaps(keywords)) { 
    ... 
} 
2

喜歡的東西

new[] { foo.Something, foo.SomethingElse, foo.AnotherThing }.Any(s => keywords.Any(w => s.Contains(w))) 
相關問題