2012-05-15 26 views
0

我正在使用SQL Server UDF來允許我在LINQ to SQL中使用CONTAINSTABLE。使用String.Join構建Linq to SQL ContainsTable搜索條件

我使用字符串數組和正則表達式將用戶的搜索字符串拆分爲可識別的單詞,然後將字符串數組重新組合爲符合CONTAINSTABLE語法的搜索條件字符串。

我想讓用戶能夠用引號括住短語。換句話說,如果用戶輸入'黃色'藍綠色'',搜索條件應該是'黃色'或'藍綠色'''根據下面的代碼,最好的處理方法是什麼?

IEnumerable<string> keywords = Regex 
    .Matches(search, @"(?<match>\w+)|\""(?<match>[\w\s]*)""") 
    .Cast<Match>() 
    .Select(m => m.Groups["match"].Value) 
    .ToList(); 

string searchCondition = ""; 
searchCondition = String.Join(" OR ", keywords); 

List = from t1 in List 
    join fts in _dc.udfSearchContent(searchCondition) 
    on t1.ContentID equals fts.ContentID 
    select t1; 

謝謝!

回答

0

.Select(x => @"""" + x + @"""")有竅門。

IEnumerable<string> keywords = Regex 
    .Matches(search, @"(?<match>\w+)|\""(?<match>[\w\s]*)""") 
    .Cast<Match>() 
    .Select(m => m.Groups["match"].Value) 
    .ToList(); 

string searchCondition = ""; 
searchCondition = String.Join(" OR ", keywords.Select(x => @"""" + x + @"""")); 

List = from t1 in List 
    join fts in _dc.udfSearchContent(searchCondition) 
    on t1.ContentID equals fts.ContentID 
    select t1;