1

I doubt this is just specific to NHibernate. But I have code as follows....

public class ClientController : ApiController
    {
        // GET /api/<controller>
        public IQueryable<Api.Client> Get()
        {
            return Repositories.Clients.Query().Select(c => Mapper.Map<Client, Api.Client>(c));
        }

I basically want to Query the database using the Odata criteria.... get the relevant 'Client' objects, and the convert them to the DTO 'Api.Client'.

But... the code as is, doesn't work. Because NHibernate doesn't know what to do the with the Mapper.... It really wants the query to come before the .Select. But I'm not sure I can get the Odata Query first?

It will work if I do

return Repositories.Clients.Query().Select(c => Mapper.Map<Client, Api.Client>(c)).ToList().AsQueryable();

But that's a bit sucky as you have to get ALL the clients from the database to do the OData query on.

Is there anyway to get the "Select" to happen after the OData query? Or another way to approach this?

1
  • actually I think the problem gets worse when you want the query to only use whats avaiable on the DTO object. You need the query to go through the mapping interface then back down to the database..... Commented Apr 24, 2012 at 5:17

3 Answers 3

1

I did not test it yet but the Open Source project NHibernate.OData could be useful for you.

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

Comments

0

The problem is that you are trying to execute C# code (Mapper.Map) inside the NH call (that is translated to SQL)

You'd have to map Api.Client manually, or create a Mapper implementation that returns an Expression<Func<Client, Api.Client>> and pass it directly as a parameter to Select().

Even with that, I'm not sure if NHibernate will translate it. But you can try.

1 Comment

yes, I know this is my problem.... I'm not sure there's really a good solution out of the box.
0

AutoMapper supports this scenario with the Queryable Extensions

public IQueryable<Api.Client> Get() {
    return Repositories.Clients.Query().Select(c => Mapper.Map<Client, Api.Client>(c));
}

becomes

public IQueryable<Api.Client> Get() {
    return Repositories.Clients.Query().ProjectTo<Api.Client>(mapper.ConfigurationProvider);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.