如何使用linq使用hibernate queryover连接两列

sem*_*gay 3 c# linq nhibernate fluent-nhibernate

我想在select子句中连接Employee的firstname和lastname,但是它给出了:

无法从新的<> 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>();
Run Code Online (Sandbox Code Playgroud)

Rad*_*ler 7

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)
Run Code Online (Sandbox Code Playgroud)

我们从sql函数concat获益.我们传递一个Projections.SqlFunctioninto Select()语句并使用一些default/basic构建部分Projections


flo*_*ler 6

或者现在甚至更简单:

using NHibernate.Criterion;

SelectList(l => l
  .Select(x => Projections.Concat(m.FirstName, ", ", m.LastName))
  .WithAlias(() => businessSectorItem.text))
)
Run Code Online (Sandbox Code Playgroud)