2014-02-22 71 views
0

我開發的新聞網站在asp.net 4.0與C#中,實體框架5 &的SQLServer 2008顯示新聞

後閱讀新聞我展示基於來自其他新聞

匹配標籤的相關新聞
var article = (from c in ctx.Articles where c.ArticleID == "10" select c).SingleOrDefault(); 
var tags = new List<string>(); 
string[] words = article.Tags.Split(','); 
foreach (string word in words) 
{ 
    if (word != "") { tags.Add(word.Trim()); } 
} 

最新消息有以下標籤: 太陽,月亮,星星

var relatedNews = (from c in ctx.Articles where tags.Contains(c.Tags) select c).Take(10); 

如果其他消息有一個標籤「,如太陽「它工作正常 但是如果它有‘太陽星系’或多個標籤,這是行不通的

回答

0

我不能完全肯定,但我認爲這個問題是:

where tags.Contains(c.Tags) 

現在文章必須提供所有提供的標籤(sun和galaxy),而不是一個或多個標籤(sun或galaxy)。這真的是你想要的還是我誤解你的問題/代碼?它不應該是這樣的:

where c.Any(x => tags.Contains(x)) 

這樣的事情應該選擇具有其中一個標籤,或兩個標籤的項目。另見:

linq where list contains any in list