gpt4 book ai didi

c# - 如何使用 Nhibernate 连接两个所有 id 均为 UNIQUEIDENTIFIER 的表来选择仅具有一个不同列的多个列

转载 作者:行者123 更新时间:2023-11-29 10:44:12 24 4
gpt4 key购买 nike

以下是我的表结构:

create table Department(
deptid [UNIQUEIDENTIFIER] ROWGUIDCOL DEFAULT NEWSEQUENTIALID() NOT NULL CONSTRAINT Department_P_KEY PRIMARY KEY,
departmentname varchar(100)
)

create table Employee(
empid [UNIQUEIDENTIFIER] ROWGUIDCOL DEFAULT NEWSEQUENTIALID() NOT NULL CONSTRAINT Employee_P_KEY PRIMARY KEY,
name varchar(50),
city varchar(50),
deptid UNIQUEIDENTIFIER NOT NULL,
foreign key(deptid) references Department(deptid) ON DELETE CASCADE ON UPDATE CASCADE
)

我的问题是,当我选择多列并在所有主键和外键均为 UNIQUEIDENTIFIER 的一列上应用 DISTINCT 时,查询会给出错误消息。我的查询和错误如下:

select distinct(Department.deptid),min(Employee.empid) as empid
from Employee
inner join Department on Department.deptid = Employee.deptid
group by Department.deptid

错误消息是:

Msg 8117, Level 16, State 1, Line 2 Operand data type uniqueidentifier is invalid for min operator.

但是如果主键和外键是 int 那么上面的查询将成功执行,因为 min 函数支持整数,但如何使用 uniqueidentifier 键选择不同的记录。

以下是我的 Nhibernate 查询:-

EmployeeEntity employeeEntity = new EmployeeEntity();
employeeEntity.DepartmentMaster = new DepartmentEntity();
ProjectionList projectionList = Projections.ProjectionList();
projectionList.Add(Projections.Distinct(Projections.Property<EmployeeEntity>(x => x.DepartmentMaster)).WithAlias(() => employeeEntity.DepartmentMaster));
projectionList.Add(Projections.Property<EmployeeEntity>(x => x.empid).WithAlias(() => employeeEntity.empid));

IList<EmployeeEntity> query = null;
query = NHSession.QueryOver<EmployeeEntity>()
.Select(projectionList)
.TransformUsing(Transformers.AliasToBean<EmployeeEntity>()).List<EmployeeEntity>();

如何按照上面的 sql 查询首先在 sql 中执行不同的查询?

最佳答案

只需将 UniqueIdentifier 转换为字符串即可

select distinct(Department.deptid),min(CAST(Employee.empid AS NVARCHAR(36))) as empid
from Employee
inner join Department on Department.deptid = Employee.deptid
group by Department.deptid

关于c# - 如何使用 Nhibernate 连接两个所有 id 均为 UNIQUEIDENTIFIER 的表来选择仅具有一个不同列的多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44920144/

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