Reputation: 5842
I am using linq to search through a list (user enters query in a textbox).
I want this to be case-insensitive and tried to use IgnoreCase, but I have no idea where to put it.... I know I could use upper or lower, but I would like to hear if anybody has any alternative methods? What would be considered best practise? Regex did not seem to work either?
string searchQuery = tbSearchQuery.Text;
var something= from x in y
where x.Subject.Contains(searchQuery)
select x;
Upvotes: 10
Views: 12241
Reputation: 5659
Since nobody else has put it up yet, I'd propose using the static String.Equals so you don't have to worry about null
and get back only the information you want.
String.Compare also works but you're not trying to sort the strings (the reason for the integer return value), just determine whether they are value-equal under case-insensitive comparison.
var something = from x in y
where string.Equals(x.Subject, searchQuery, StringComparison.CurrentCultureIgnoreCase)
select x;
Upvotes: 7
Reputation: 2532
I second the extension method way. I've got a class I use. Just plonk it in your project and reference YOUR NAME SPACE and away you go.
using System;
namespace YOUR NAME SPACE
{
public static class StringExtensionMethods
{
/// <summary>
/// Extention method to allow a string comparison where you can supply the comparison type
/// (i.e. ignore case, etc).
/// </summary>
/// <param name="value">The compare string.</param>
/// <param name="comparisionType">The comparison type - enum, use OrdinalIgnoreCase to ignore case.</param>
/// <returns>Returns true if the string is present within the original string. </returns>
public static bool Contains(this string original, string value, StringComparison comparisionType)
{
return original.IndexOf(value, comparisionType) >= 0;
}
}
}
And here's an example of a LINQ query using it:
var results = from promotion in response
where
String.IsNullOrEmpty(find.Code) ||
(promotion.Code != null && promotion.Code.Contains(find.Code, StringComparison.OrdinalIgnoreCase))
where
String.IsNullOrEmpty(find.Name) ||
(promotion.Name != null && promotion.Name.Contains(find.Name, StringComparison.OrdinalIgnoreCase))
select promotion;
Upvotes: 0
Reputation: 24236
Could you use IndexOf
instead?
string searchQuery = tbSearchQuery.Text;
var something= from x in y
where x.Subject.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) >= 0
select x;
Upvotes: 0
Reputation: 111930
If you are using LINQ-to-SQL
or Entity Framework
, the collation rules are fixed and set in the table definition. The only way to compare them is to ToUpper
(or ToLower
, but it's better to ToUpper
as written here http://www.siao2.com/2007/10/01/5218976.aspx) both part of the comparison.
Upvotes: 0
Reputation: 4754
Use string.Compare:
var something= from x in y
where string.Compare(x.Subject, searchQuery, true) >= 0
select x;
This can also handle situations where either strings are null.
Upvotes: -1
Reputation: 22255
string searchQuery = tbSearchQuery.Text;
var something= from x in y
where x.Subject.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) >= 0
select x;
Upvotes: 5
Reputation: 5422
Try
string searchQuery = tbSearchQuery.Text;
var something= from x in y
where x.Subject.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) != -1
Upvotes: 0
Reputation: 6873
I use the following own made extensions (for plain strings)
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source.IndexOf(toCheck, comp) >= 0;
}
public static bool ContainsIgnoreCase(this string source, string toCheck)
{
return source.IndexOf(toCheck, StringComparison.InvariantCultureIgnoreCase) >= 0;
}
HTH
Upvotes: 2