In the development of an Orchard module, how do I retrieve ContentParts case insensitively filtered by a field? I have tried
var name = viewModel.Name.ToUpper();
var samples = _contentManager.Query<SamplePart, SamplePartRecord>()
.Where(x => x.Name.ToUpper() == name)
.List();
and I'm getting an error
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
but when I tried to retrieve without bothering if it's case sensitive
var name = viewModel.Name;
var samples = _contentManager.Query<SamplePart, SamplePartRecord>()
.Where(x => x.Name == name)
.List();
No errors reported.
What gives?
Be aware that the expression inside the Where
clause is being translated by NHibernate to an SQL query at some point. Hence you're pretty restricted to what you can do there. In this case it seems like the ToUpper
method is not supported.
Another thing - the string comparison behavior in SQL Server depends on the actual collation set on your database. By default it's case insensitive, so any string comparison will ignore case. So, if you stick to defaults you're good with just an ordinary ==
on two strings, like in your last example.