Wednesday, November 9, 2011

watching what NHibernate does in the Output pane

Following a string with a .Contains in a LinqSpec implementation as suggested here...

var programNameSpec = new AdHocSpecification<Program>(p => p.Name.ToLower().Contains(pieceOfName.ToLower()));

 
 

...negates the need to cast strings to .ToLower or .ToUpper to ensure case-insensitive matching as NHibernate will use percentage sign ("like") matching as shown in this line which I got in my Output pane in Visual Studio by entering a capital letter A in one of many fields for filtering a list of an object called Program and then submitting the related form.

NHibernate.SQL: 2011-11-09 10:21:30,784 [21] DEBUG NHibernate.SQL [(null)] - select program0_.Id as col_0_0_ from Programs program0_ where case when program0_.IsRetired=1 then 'true' else 'false' end=case when @p0='true' then 'true' else 'false' end and (lower(program0_.Name) like ('%'+@p1+'%'));@p0 = 'False' [Type: String (4000)], @p1 = 'a' [Type: String (4000)]

 
 

Using a capital letter A for pieceOfName, this filtered the same initial result set to the same constrained result set as the spec above:

var programNameSpec = new AdHocSpecification<Program>(p => p.Name.Contains(pieceOfName));

 
 

The NHibernate stuff is only marginally different:

NHibernate.SQL: 2011-11-09 10:31:23,655 [16] DEBUG NHibernate.SQL [(null)] - select program0_.Id as col_0_0_ from Programs program0_ where case when program0_.IsRetired=1 then 'true' else 'false' end=case when @p0='true' then 'true' else 'false' end and (program0_.Name like ('%'+@p1+'%'));@p0 = 'False' [Type: String (4000)], @p1 = 'A' [Type: String (4000)]

No comments:

Post a Comment