Using Linq-To-SQL I'm getting some weird behavior doing text searches with the .Contains method. Loo

Posted by Nate Bross on Stack Overflow See other posts from Stack Overflow or by Nate Bross
Published on 2010-04-16T16:01:27Z Indexed on 2010/04/16 16:23 UTC
Read the original article Hit count: 197

I have a table, where I need to do a case insensitive search on a text field.

If I run this query in LinqPad directly on my database, it works as expected

Table.Where(tbl => tbl.Title.Contains("StringWithAnyCase"))
// also, adding in the same constraints I'm using in my repository works in LinqPad
// Table.Where(tbl => tbl.Title.Contains("StringWithAnyCase") && tbl.IsActive == true)

In my application, I've got a repository which exposes IQueryable objects which does some initial filtering and it looks like this

var dc = new MyDataContext();

public IQueryable<Table> GetAllTables()
{
    var ret = dc.Tables.Where(t => t.IsActive == true);
    return ret;
}

In the controller (its an MVC app) I use code like this in an attempt to mimic the LinqPad query:

var rpo = new RepositoryOfTable();
var tables = rpo.GetAllTables();
// for some reason, this does a CASE SENSITIVE search which is NOT what I want.
tables = tables.Where(tbl => tbl.Title.Contains("StringWithAnyCase");
return View(tables); 

The column is defiend as an nvarchar(50) in SQL Server 2008.

Any help or guidance is greatly appreciated!

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-sql