我有这两个表
APPUSER
PK:User_Id
UserPrinciples
User_ID(不是此表中的键)
用户名
SELECT TOP(page_size)
FROM AppUser this_ left join
(select UserPrinciples.*
from (select UserPrinciples.*,
row_number() over (partition by UserName order by User_Id) as seqnum
from UserPrinciples
) UserPrinciples
where seqnum = 1) as b
on this_.User_Id=b.User_ID
( AppUser由IUser重新播放; UserPrinciples由StoredCredentials重播)
public UserPagedQuery(ISessionProvider<ISession> sessionProvider)
{
_sessionProvider = sessionProvider;
_query = sessionProvider.Session.QueryOver<IUser>();
var uc = _query.UnderlyingCriteria;
var ap = uc.GetOrCreateAssociationPath("StoredCredentials");
ap.AddOrder(Order.Asc("UserName"));
}
public IEnumerable<IUser> Execute()
{
var pagedReceiving = _query.TransformUsing(Transformers.DistinctRootEntity).List<IUser>();
return pagedReceiving;
}
SELECT TOP(page_size) FROM AppUser left outer join UserPrinciples
on AppUser.User_Id=UserPrinciples.User_ID
ORDER BY UserPrinciples.UserName asc
并且这还不够好,因为我只需要每个user_id中的一条记录,而对于某些用户,使用此查询我可以获得多条记录,这就是问题所在。
任何人都可以帮忙吗? 不确定是否需要任何其他细节
感谢和最好的问候
答案 0 :(得分:0)
表示为没有分区的SQL
SELECT TOP(page_size) up.*, au.* from
(SELECT UserName, Min(User_Id) as User_Id FROM UserPrinciples GROUP BY UserName) as x
LEFT JOIN
UserPrinciples up ON x.UserName = up.UserName AND x.User_Id = up.User_Id
LEFT JOIN
AppUser au ON au.User_Id = up.User_Id
翻译成linq的应该是符合条件的:
var y = from up1 in session.Query<Employment>()
group up1 by up1.Name into g
select new { Name = g.Key, UserId = g.Min(x => x.Company.Id) } into f
join up2 in session.Query<Employment>() on new { f.Name, f.UserId } equals new { up2.Name, UserId = up2.Company.Id }
select new SomeDto
{
ProfileName = up2.Name,
UserId = up2.User.Id,
Name = up2.User.Name,
};
var results = y.ToList();