如何在NHibernate中获取带有id的项目列表
本文关键字:id 项目 列表 获取 NHibernate | 更新日期: 2023-09-27 18:15:19
我有一个项目id列表playerIds
(他们来自Redis),我想通过NHibernate获得对象(User
),但我写的方式并没有优化它认为。
List<int> playerIds = redisRespnse.Select(x => int.Parse(x)).ToList();
using (var session = sessionFactory.OpenSession())
{
List<User> TopPlayers = new List<User>();
for (int i = 0; i < playerIds.Count; i++)
{
var player = session.Query<User>()
.Where(user => user.Id == playerIds[i])
.FirstOrDefault();
if (player != null)
{
TopPlayers.Add(player);
}
}
var finalResponse = new GetLeaderBoardResponse(TopPlayers);
return Ok(finalResponse);
}
谢谢。
Nhibernate linq提供程序支持。contains
List<int> playerIds = redisRespnse.Select(x => int.Parse(x)).ToList();
using (var session = sessionFactory.OpenSession())
{
var players = session.Query<User>()
.Where(user => playerIds.Contains(user.Id))
.Select(user => user).ToList();
}
var finalResponse = new GetLeaderBoardResponse(players);
return Ok(finalResponse);
}