gpt4 book ai didi

linq - Nhibernate 中的子查询

转载 作者:行者123 更新时间:2023-12-03 11:29:34 25 4
gpt4 key购买 nike

我有以下结构。

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
}

休眠文件

群成员
            <?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)

什么是 nhibernate 语法? 请急...

最佳答案

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

关于linq - Nhibernate 中的子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18208199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com