问题描述
我在弄清linq查询的Where子句中使用string.compare的确切语法时遇到了一些麻烦.以下是我到目前为止的内容.
I am having a bit of trouble figuring out the exact syntax to use string.compare in the Where clause of a linq query. Below is what I have so far.
filteredApplications = AllApplications.Where(x => x.Name.Contains(string.Compare(x.Name, txtSearch.Text, StringComparison.OrdinalIgnoreCase))).ToList();
这是否有可能,或者我吠错了树吗?
Is this even possible or am I barking up the wrong tree?
隆达
推荐答案
如果要检查Name
是否包含搜索文本:
If you want to check to see if Name
contains the search text:
AllApplications.Where(x => x.Name.ToUpperInvariant().Contains(txtSearch.Text.ToUpperInvariant()))).ToList();
如果要检查是否相等:
AllApplications.Where(x => string.Equals(x.Name, txtSearch.Text, StringComparison.OrdinalIgnoreCase)).ToList();
在原始查询中,您正在检查x.Name
是否包含string.Compare
的结果.我认为您不是要这样做,因为 string.Compare
返回一个整数. string.Compare
主要用于确定排序顺序.
In your original query, you were checking to see if x.Name
contains the result of string.Compare
. I assume you weren't trying to do this, since string.Compare
returns an integer. string.Compare
is used primarily for determining sort order.
这篇关于在linq查询where子句中使用string.compare的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!