English 中文(简体)
NHibern Query 单独
原标题:NHibernate QueryOver distinct

I have this scenario:

class User
{
Id,
UserName
}

class UserRelationship
{
User GroupUser,
User MemberUser
}

and query

var query = QueryOver.Of<UserRelationship>()
.JoinqueryOver(x=>x.MemberUser)
.Where(x=>x.UserName == "TestUser");

我现在要归还名单尊敬的用户,因此我不能这样做。

变迁(变迁者,异本能

因为这将给我带来用户关系。

我需要这样的东西:

Select distinct user.ID 
from UserRelationship relationship
inner join User user on user.ID = relationship.MemberUser_ID

Please help thanks

最佳回答

班级:

public class User
{
    public virtual int Id {get; set;}
    public virtual string UserName {get; set;}
}

public class UserRelationship
{
    public virtual int Id {get; set;}
    public virtual User GroupUser {get; set;}
    public virtual User MemberUser {get; set;}
}

并且:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x=>x.Id).GeneratedBy.Native();
        Map(x=>x.UserName);
    }
}

public class UserRelationshipMap : ClassMap<UserRelationship>
{
    public UserRelationshipMap(){
        Id(x=>x.Id).GeneratedBy.Native();
        References(x=>x.GroupUser);
        References(x=>x.MemberUser);
    }
}

您希望从用户类别中检索一份基于“成员”的单独“用户名称”清单。

var distinctMemberUsers = QueryOver.Of<UserRelationship>()
    .Select(x => x.MemberUser.Id);

var users = QueryOver.Of<User>()
    .WithSubquery.WhereProperty(x=>x.Id).In(distinctMemberUsers)

这应当使用文件库中的一条条款,给你一个单独的用户清单。

问题回答

我知道这一职位是老的,但我刚刚遇到同样的问题,我认为我会同意我认为更简单的答复。

不管怎样,NHibernate将不得不对每一母物体的多行提出质疑(除非你使用替代物而不是使用Join)。 因此,我们知道,当确实只有100个独特物体时,我们会再次获得500个目标。

由于这些物体已经被问,而且已经记忆中——为什么不使用准则?

基于这个问题: LINQ 特定财产的区别 最加之的答案提供了非常雄辩的解决办法。 编制另一个清单,并且有准则比较。 如果我们能够在数据库中独一无二,那显然是更好的选择,但是,由于这并非一种选择,准则似乎是一个好的解决办法。





相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签