問題描述
為什么是這條線:
var category = _dataContext.Categories.Where<Category>(p => p.Keywords.Split(' ').Contains<string>(context.Request.QueryString["q"])).First();
拋出一個(gè) System.NotSupportedException:
throws an System.NotSupportedException:
類型System.String[]"不支持比較運(yùn)算符
Comparison operators not supported for type 'System.String[]'
我該如何解決?謝謝.
推薦答案
所以您正在數(shù)據(jù)庫(kù)中以空格分隔的列中尋找值(來自查詢字符串)?并且您正在使用 Split
來查詢數(shù)據(jù)庫(kù)中的各個(gè)值?
So you are looking for a value (from the query-string) in a space-delimited column in the database? And you're using Split
to query the individual values inside the database?
(只是檢查我的假設(shè)...)
(just checking my assumptions...)
string.Split
不支持這種方式(在列數(shù)據(jù)的數(shù)據(jù)庫(kù)中) - 請(qǐng)參閱此處了解 支持的字符串操作.(注意 string.Split
顯式不被支持).
string.Split
is not supported in this way (at the database on column data) - see here for the supported string operations. (note that string.Split
is explicitly not supported).
我很懶;當(dāng)我在數(shù)據(jù)庫(kù)中對(duì)數(shù)據(jù)進(jìn)行分隔時(shí)(比較少見),我總是在數(shù)據(jù)的開頭和結(jié)尾添加相同的分隔符;然后我可以搜索:
I'm lazy; when I delimit data in the database (relatively rare), I always add the same delimiter to the start and end of the data; then I can just search for:
string searchFor = DELIMITER + searchValue + DELIMITER;
...
.Where(row => row.Value.Contains(searchFor));
然而;在這種情況下,我希望最實(shí)用的選擇可能是編寫一個(gè) UDF 函數(shù)來搜索分隔的 varchar
(正確處理第一個(gè)/最后一個(gè)項(xiàng)目),并在數(shù)據(jù)上下文中公開 UDF - 然后使用:
However; in this case, I expect the most practical option might be to write a UDF function that searches a delimited varchar
(correctly handling the first/last item), and expose the UDF on the data-context - then use:
.Where(row => ctx.ContainsValue(row.Value, searchValue)); // ContainsValue is our UDF
或者 - 標(biāo)準(zhǔn)化數(shù)據(jù)...
Or - normalise the data...
.Where(row => row.Values.Any(s=>s.Value == searchValue));
這篇關(guān)于類型“System.String[]"不支持比較運(yùn)算符的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!