如何使用 Nhibernate select 多列只有一个不同的列来连接两个所有 id 都是 UNIQUEIDENTIFIER 的表

how to select multiple columns with only one distinct column from joining two tables with all id's are UNIQUEIDENTIFIER using Nhibernate

以下是我的table结构:

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
    )

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

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 那么上面的查询将成功执行,因为最小函数支持整数但是我如何 select 使用 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