在linq查询where子句中使用string.compare [英] Using string.compare in a linq query where clause

查看:106
本文介绍了在linq查询where子句中使用string.compare的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆