3

I want to concat Employee firstname and lastname at select clause but it gives :

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>();
6
  • string.Format has a lot of different options that is too complex for most SQL engines to accomplish, try using x.FirstName + " " + x.LastName Commented Aug 30, 2014 at 14:11
  • i just tried your suggestion but it still gives the same error Commented Aug 30, 2014 at 14:14
  • It is also possible that query engine cannot understand anonymous type of new { name = string.Format("{0} {1}", x.FirstName, x.LastName) }. I am not familiar with fluent-nhibernate, but maybe you can use string directly string.Format("{0} {1}", x.FirstName, x.LastName)? How will it interoperate with following WithAlias I don't know, but, nonetheless, it is a fair guess to check. Commented Aug 30, 2014 at 14:16
  • @EugenePodskal what do you mean by using string directly. If you mean that Select(x => x.FirstName+" "+x.LastName) it is also not working Commented Aug 30, 2014 at 14:20
  • Well, that was a fair, but wrong guess. Have you tried just to select x.FirstName or just x.LastName? Will it work? Commented Aug 30, 2014 at 14:22

2 Answers 2

8

The QueryOver syntax would look like this:

// 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)

We profit here from a sql function concat. We pass a Projections.SqlFunction into Select() statement and build part using some default/basic Projections

Sign up to request clarification or add additional context in comments.

2 Comments

NHibernate is great! Enjoy it ;)
Entity forever.:) boss made me use NHibernate :/
7

Or now even simpler:

using NHibernate.Criterion;

SelectList(l => l
  .Select(x => Projections.Concat(m.FirstName, ", ", m.LastName))
  .WithAlias(() => businessSectorItem.text))
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.