我创建了一个查询,该查询将多个表与结尾处的 where 连接起来,最终这些表将按照 ormlite 自述文件中的建议映射到自定义数据模型。
using (var db = _connectionFactory.Open()) {
var q = db.From<PlayerMatch>()
.Join<PlayerMatch, Player>((x, y) => x.PlayerId == y.Id)
.Join<PlayerMatch, Team>((x, y) => x.TeamId == y.Id)
.Join<PlayerMatch, Match>((x, y) => x.MatchId == y.Id)
.Join<PlayerMatch, Goal>((x, y) => x.Id == y.PlayerMatchId)
.Where<PlayerMatch>(x => x.MatchId == matchId)
.Select<PlayerMatch, Player, Team, Match, Goal>((pm, p, t, m, g) => new { PlayerMatchId = pm.Id, PlayerId = p.Id, TeamId = t.Id, MatchId = m.Id, TotalGoalsScored = Sql.Count(g.Id) } );
var result = await db.SelectAsync<PlayerMatchEndResult>(q);
result.PrintDump();
return result;
但是,在我的测试中,我发现即使 where 子句中的 matchid 不存在,它仍然返回以下数据模型:
PlayerMatchEndResult
{
MatchId = 0
PlayerId = 0
PlayerMatchId = 0
TeamId = 0
TotalGoalsScored = 0
}
正如你所看到的,因为它找不到相应的记录,所以它默认匿名对象的所有属性为0,从而返回一个无效的对象。
我想让它不返回任何内容(null),这样我就可以根据 null 检查而不是比较 if id == 0 来检查记录是否存在。
这可以用 Ormlite 实现吗?
最佳答案
Enabling logging正如 Mythz 所建议的那样解决了我的问题。
关于c# - servicestack ormlite 总是返回匿名对象,即使没有找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44632320/