如何使用linq将两列与hibernate queryover连接
本文关键字:两列 hibernate queryover 连接 linq 何使用 | 更新日期: 2023-09-27 18:20:13
我想在select子句中连接Employee的名字和姓氏,但它给出了:
无法从新<>中确定成员f_AnonymousType0`1(名称=格式("{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
构建部件
或者现在更简单:
using NHibernate.Criterion;
SelectList(l => l
.Select(x => Projections.Concat(m.FirstName, ", ", m.LastName))
.WithAlias(() => businessSectorItem.text))
)