我需要做一個簡單的搜索引擎。我發現這個Microsoft example幫助我開始。現在,我的問題是編寫這個函數在LINQ到SQL,所以我可以使用它與我的dbcontext應用程序。Linq to SQL用於搜索查詢
/// <summary>
/// Search records from database.
/// </summary>
/// <param name="keywords">the list of keywords</param>
/// <returns>all found records</returns>
public List<Article> Search(List<string> keywords)
{
// Generate a complex Sql command.
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.Append("select * from [Articles] where ");
foreach (string item in keywords)
{
sqlBuilder.AppendFormat("([Title] like '%{0}%' or [Content] like '%{0}%') and ", item);
}
// Remove unnecessary string " and " at the end of the command.
string sql = sqlBuilder.ToString(0, sqlBuilder.Length - 5);
return QueryList(sql);
}
我在以前的創建自己的函數庫:
public List<Record> Search(string[] keywords)
{
var result = (from c in _context.Tenders
where keywords.Contains(c.Title) || keywords.Contains(c.Summary)
select new Record()
{
Id = c.Id,
Title = c.Title,
Content = c.Summary
}).ToList();
return result;
}
但搜索結果是不一樣的。如果我搜索合同在在鎮上的新合同我不會得到結果。我需要寫完整句子才能得到它。你能幫我指出什麼是在我的LINQ到SQL功能實現嗎?
非常感謝!