Nhibernate 基于可空字符串比较的筛选

Nhibernate 基于可空字符串比较的筛选,nhibernate,queryover,Nhibernate,Queryover,我正在使用nHibernate搜索不匹配的字符串 模型是这样的: PlayerGroup有一个字段ExpectedPlayKey Player有一个字段LastReportedPlayKey 一个PlayerGroup有许多玩家 我想执行查询以查找与该组预期播放列表不匹配的所有播放器 我的代码如下: PlayerGroup playerGroupAlias = null; Player playerAlias = null; var query = this.Session.QueryOv

我正在使用nHibernate搜索不匹配的字符串

模型是这样的:

  • PlayerGroup
    有一个字段
    ExpectedPlayKey

  • Player
    有一个字段
    LastReportedPlayKey

  • 一个
    PlayerGroup
    有许多
    玩家

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

我的代码如下:

PlayerGroup playerGroupAlias = null;
Player playerAlias = null;

var query = this.Session.QueryOver<Player>(() => playerAlias)
                        .JoinAlias(() => playerAlias.PlayerGroup, () => playerGroupAlias)
                        .Where(
                                () => (playerGroupAlias.ExpectedPlaylistKey != playerAlias.CurrentlyReportedPlaylistKey)
                              );
不幸的是,如果其中一个值为NULL,则返回false,即使另一个值不是NULL


我如何修复我的nHibernate查询,以便它处理任意一个字符串为空的情况?

我已经找到了一个可行的答案

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

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
            )
     );
var query=this.Session.QueryOver(()=>playerAlias)
JoinAlias先生(
()=>playerAlias.PlayerGroup,
()=>PlayerGroup别名
).在哪里(
() => 
(
(playerAlias.CurrentlyReportedPlaylistKey==null)
&&(playerGroupAlias.ExpectedPlayKey!=null)
)
|| (
(playerAlias.CurrentlyReportedPlaylistKey!=null)
&&(playerGroupAlias.ExpectedPlayKey==null)
)
|| (
playerGroupAlias.ExpectedPlayKey!=playerAlias.CurrentlyReportedPlaylistKey
)
);
如您所见,我使用了一个lambda表达式,它由五个比较以及五个其他布尔运算组成,所有这些都是为了问“这两个字符串不同吗?”


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

我已经想出了一个有效的答案

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

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
            )
     );
var query=this.Session.QueryOver(()=>playerAlias)
JoinAlias先生(
()=>playerAlias.PlayerGroup,
()=>PlayerGroup别名
).在哪里(
() => 
(
(playerAlias.CurrentlyReportedPlaylistKey==null)
&&(playerGroupAlias.ExpectedPlayKey!=null)
)
|| (
(playerAlias.CurrentlyReportedPlaylistKey!=null)
&&(playerGroupAlias.ExpectedPlayKey==null)
)
|| (
playerGroupAlias.ExpectedPlayKey!=playerAlias.CurrentlyReportedPlaylistKey
)
);
如您所见,我使用了一个lambda表达式,它由五个比较以及五个其他布尔运算组成,所有这些都是为了问“这两个字符串不同吗?”

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