2011-10-18 77 views
2

我正在使用nHibernate来搜索不匹配的字符串。过滤比较可空字符串

的模式是这样的:

  • PlayerGroup有一个字段ExpectedPlaylistKey

  • Player有一个字段LastReportedPlaylistKey

  • 其中一个PlayerGroup有很多Players

我想执行一个查询来查找所有与该组预期播放列表不匹配的播放器。

我的代码如下:

PlayerGroup playerGroupAlias = null; 
Player playerAlias = null; 

var query = this.Session.QueryOver<Player>(() => playerAlias) 
         .JoinAlias(() => playerAlias.PlayerGroup,() => playerGroupAlias) 
         .Where(
           () => (playerGroupAlias.ExpectedPlaylistKey != playerAlias.CurrentlyReportedPlaylistKey) 
          ); 

我已经研究了生成的SQL,并且它使用这个where子句:

WHERE not (playergrou1_.ExpectedPlaylistKey = this_.CurrentlyReportedPlaylistKey) 

不幸的是,如果这些值之一为NULL,则此即使其他值不为null,也会返回false。

我该如何解决我的nHibernate查询,所以它处理的情况下,如果任何字符串是NULL?

回答

0

我想出了一个可行的答案。

但是,考虑到问题的简单性,这看起来非常笨拙的代码。

var query = this.Session.QueryOver<Player>(() => playerAlias) 
         .JoinAlias(() => playerAlias.PlayerGroup,() => playerGroupAlias) 
         .Where(
           () => (
             (playerAlias.CurrentlyReportedPlaylistKey == null) 
              && 
             (playerGroupAlias.ExpectedPlaylistKey != null) 
            ) 
             || 
             (
             (playerAlias.CurrentlyReportedPlaylistKey != null) 
              && 
             (playerGroupAlias.ExpectedPlaylistKey == null) 
            ) 
             || 
             (
             playerGroupAlias.ExpectedPlaylistKey != playerAlias.CurrentlyReportedPlaylistKey 
            ) 

            ); 

正如你所看到的,我已经使出由五个比较lambda表达式以及其他五个布尔运算,所有要问的问题“不同这两个字符串”?

我希望有一个更优雅的解决方案。