Problem with string.Contains()

R

Raja

//I have a case sensitive problem with contains method.
//When using SQL database as here:
MERADATADataContext DC = new MERADATADataContext();
IQueryable<DVD> results = from dvd in DC.DVDs
where dvd.Title.Contains(txtTitle.Text.Trim())
select dvd;
//then Contains method works fine:
//================================================
//But when using XML as:
XElement doc = XElement.Load(fileName);
var results = from h in doc.Descendants("XXX")
where h.Element("YYY").Value.Contains(strYYY)
...
//then Contains method is case sensitive. Probably it is because XML
is case sensitive.
//If true, then what is the work around to find data case
insensitively?

Thanks in advance
Raja
 
M

Martin Honnen

Raja said:
//I have a case sensitive problem with contains method.
//When using SQL database as here:
MERADATADataContext DC = new MERADATADataContext();
IQueryable<DVD> results = from dvd in DC.DVDs
where dvd.Title.Contains(txtTitle.Text.Trim())
select dvd;
//then Contains method works fine:
//================================================
//But when using XML as:
XElement doc = XElement.Load(fileName);
var results = from h in doc.Descendants("XXX")
where h.Element("YYY").Value.Contains(strYYY)
...
//then Contains method is case sensitive. Probably it is because XML
is case sensitive.
//If true, then what is the work around to find data case
insensitively?

It is not the case-sensitivity of XML that matters (as that only relates
to element and attribute names), it is the Contains methods of
System.String that matters: "This method performs an ordinal
(case-sensitive and culture-insensitive) comparison".

If you want to do a case-insensitive comparison then you can use a
regular expression e.g.

XDocument doc = XDocument.Parse(@"<root>
<foo>bar</foo>
<foo>baz</foo>
<foo>BAR</foo>
</root>");
foreach (XElement foo in doc.Descendants("foo").Where(f =>
Regex.IsMatch(f.Value, "bar", RegexOptions.IgnoreCase)))
{
Console.WriteLine(foo);
}
 
J

Jon Skeet [C# MVP]

//I have a case sensitive problem with contains method.
//When using SQL database as here:
MERADATADataContext DC = new MERADATADataContext();
IQueryable<DVD> results = from dvd in DC.DVDs
where dvd.Title.Contains(txtTitle.Text.Trim())
select dvd;
//then Contains method works fine:

Well, in fact it's *not* working fine - it's not behaving the same on
SQL server as it would be in normal code. String.Contains is case-
sensitive. My guess is that your database is set to be case-
insensitive, hence why it's behaving how you want it to.

To get a case-insensitive Contains equivalent in normal code (which
includes LINQ to XML - the delegate is being executed locally with no
translation) you can either use a regular expression as Martin
suggested (but be careful to escape it appropriately) or use
String.IndexOf with an appropriate StringComparison (e.g.
CurrentCultureIgnoreCase).

It's a shame that String doesn't contain an overload of Contains which
takes a StringComparison, but you could always add one as an extension
method.

Jon
 
R

Raja

Thank you people for your quick reply.
RegEx is working perfectly for what I need.

Thanks
Raja
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top