4

我有以下结构。

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) 系统用户.cs

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

休眠文件

小组成员

            <?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>

系统用户

        <?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>

我想使用这个查询得到结果

select * from sys_def_user where id not in (select UserId from SYS_DEF_GROUP_MEMBERS where GroupID =5)

什么是休眠语法呢?请尽快...

4

1 回答 1

7

您可以为此使用 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 查询。

于 2013-08-16T00:31:07.607 回答