编程语言
首页 > 编程语言> > c# – 如何使用linq使用hibernate queryover连接两列

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

作者:互联网

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

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()语句,并使用一些默认/基本投影构建部件

标签:c,linq,nhibernate,fluent-nhibernate
来源: https://codeday.me/bug/20190714/1459436.html