加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 00:24:42 所属栏目:百科 来源:网络整理
导读:我想在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.QueryOverEmployee() .O
我想在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.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.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()语句,并使用一些默认/基本投影构建部件

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读