Nhibernate中的子查询

时间:2013-08-13 11:42:32

标签: linq nhibernate

我有以下结构。

1)GroupMember.cs

    public class GroupMember
    {        
        public virtual int Id { get; set; }
        public virtual SystemUser User { get; set; }
        public virtual Group Group { get; set; }
        public virtual IList<Group> GroupDetail { get; set; }
        public virtual IList<SystemUser> SystemUserDetail { get; set; }

       // Few more Properties are there
    }        

2)SystemUser.cs

   public class SystemUser
   {       
       public virtual int Id{get;set;}            
       public virtual string DisplayName{get;set;}            
       // Few more Properties are there
   }

Nhibernate文件

GroupMembers

            <?xml version="1.0" encoding="utf-8" ?>
            <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NSP.DataModel"        
            namespace="NSP.DataModel.Account">
            <class name="GroupMember" entity-name="SysGroupMember" table="SYS_DEF_GROUP_MEMBERS">
            <id name="Id" column="id" type="Int32">
              <generator class="identity"/>
            </id>

            <many-to-one entity-name="SysGroup" name="Group" column="GroupID" not-null="true" cascade="none"/>
            <many-to-one entity-name="SysUser" name="User" column="UserID" not-null="false" cascade="none"/>

            <property name="Status" type="int" not-null="false">
              <column name="Status" not-null="false"/>
            </property>

            <property name="CreatedDate" type="datetime" not-null="false">
              <column name="CreatedDate"/>
            </property>

            <property name="CreatedBy" type="int" not-null="false">
              <column name="CreatedBy"/>
            </property>
            <property name="UpdatedDate" type="datetime" not-null="false">
              <column name="UpdatedDate"/>
            </property>
            <property name="UpdatedBy" type="int" not-null="false">
              <column name="UpdatedBy"/>
            </property>

            <bag name="GroupDetail" inverse="true">
              <key column="Id"/>
              <one-to-many entity-name="SysGroup"/>
            </bag>
            <bag name="SystemUserDetail">
              <key column="Id"/>
              <one-to-many entity-name="SysUser"/>
            </bag>
          </class>
        </hibernate-mapping>

SysUser

        <?xml version="1.0" encoding="utf-8" ?>
        <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NSP.DataModel" namespace="NSP.DataModel.Authentication">
          <class name="SystemUser" entity-name="SysUser" table="SYS_DEF_USER" abstract="true">
            <id name="Id" column="id" type="Int32">
              <generator class="identity"/>
            </id>
            <many-to-one entity-name="SysUserTypes" name="UserTypeId" column="UserTypeId" not-null="true" cascade="none" />
            <property name="IsActive" column="IsActive" type="Boolean" not-null="true"/>
            <property name="IsLicensed" column="IsLicensed" type="Boolean" not-null="true"/>
            <property name="DisplayName" type="string" not-null="false">
              <column name="DisplayName" length="128"/>

            </property>
            <property name="Email" column="Email" type="string" not-null="true" length="200"/>
            <property name="PasswordMD5HexHash" column="PasswordMD5HexHash" type="string" not-null="false"/>

         <bag name="UserTypeList" inverse="true">
              <key column="UserTypeId"/>
              <one-to-many entity-name="SysUserTypes"/>
            </bag>

          </class>

    </hibernate-mapping>

我想使用此查询获得结果

从sys_def_user中选择*,其中id不在(从SYS_DEF_GROUP_MEMBERS中选择UserID,其中GroupID = 5)

这可能是什么nhibernate语法? 请紧急......

1 个答案:

答案 0 :(得分:6)

您可以使用NHibernate的LINQ提供程序。这里列出的例子http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b是学习LINQ的一个很好的起点。这些示例中的大多数都是用from ... select语法编写的,但我更喜欢扩展方法语法:

var subquery = session.Query<GroupMember>()
    .Where(gm => gm.Group.Id == 5)
    .Select(gm => gm.User.Id);
var users = session.Query<User>()
    .Where(u => !subquery.Contains(u.Id));

希望这个例子可以帮助您开始正确的轨道,并且您将立即编写自己的LINQ查询。