gpt4 book ai didi

c# - 如何使用 linq 使用 hibernate queryover 连接两列

转载 作者:行者123 更新时间:2023-11-30 13:22:20 25 4
gpt4 key购买 nike

我想在 select 子句中连接员工的名字和姓氏,但它给出了:

Could not determine member from new <>f__AnonymousType0`1(name = Format("{0} {1}", x.FirstName, x.LastName))

var returnData = UnitOfWork.CurrentSession.QueryOver<Employee>()
.OrderBy(x => x.Id).Asc
.SelectList(u => u.Select(x => x.Id).WithAlias(() =>
businessSectorItem.id)
.Select(x => new { name = string.Format("{0} {1}",
x.FirstName, x.LastName) })
.WithAlias(() => businessSectorItem.text))
.Where(x => (x.FirstName.IsInsensitiveLike
("%" + searchTerm + "%") ||
x.LastName.IsInsensitiveLike
("%" + searchTerm + "%")) &&
( x.Account == null || x.Account.Id ==
accountId))
.TransformUsing(Transformers
.AliasToBean<SearchEmployeeItemDto>())
.Take(limit)
.List<SearchEmployeeItemDto>();

最佳答案

QueryOver 语法如下所示:

// instead of this
.Select(x => new { name = string.Format("{0} {1}",
x.FirstName, x.LastName) })
.WithAlias(() => businessSectorItem.text))

// we should use this
.Select(
Projections.SqlFunction("concat",
NHibernateUtil.String,
Projections.Property<Employee>(e => e.FirstName),
Projections.Constant(" "),
Projections.Property<Employee>(e => e.LastName)
)).WithAlias(() => businessSectorItem.text)

我们从 SQL 函数 concat 中获益。我们将 Projections.SqlFunction 传递到 Select() 语句中,并使用一些默认/基本的 Projections

构建部分

关于c# - 如何使用 linq 使用 hibernate queryover 连接两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25583153/

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