Iam have run into a little problem with a partial projection.
I have an method on our listboxes so that we can easily load entities into them, however loading full entities is of course not an option.
Meaning i have to do some projection, i have done this here where you can choose which Property to have as the DisplayValue.
Using the code looks like this:
dropCompany.LoadEntityList(Customer.Company, x => x.CompanyName, x => x.IsCompany);
Part of the implementation looks like this:
public void LoadEntityList<T>(T selectedEntity,
System.Linq.Expressions.Expression<Func<T, string>> selector,
System.Linq.Expressions.Expression<Func<T,bool>> where = null)
where T : FlexyBook.Infrastructure.Entity
{
var wCollection = new ObjectWrapperCollection<object>();
IQueryable<T> query = FlexyBook.Repository.DomainService<T>.GetDomainService().GetAll();
if (where != null)
query = query.Where(where);
foreach (var item in query.Select(x =>
new ObjectWrapper<object>()
{
Value = x.ID,
Text = selector.Compile()(x)
})
.ToList().OrderBy(x => x.Text))
{
wCollection.List.Add(item);
}
}
The problem is that this results in the full entity being loaded from the database and then projected.
The issue is also very obvious it is this line “selector.Compile()(x)”.
My problem is i have no idea how to allow this partial select to carry on into NHibernate as a projection.
I obviously need the ID selector for identification of the entities and i dont want to change the way the method is called.
Is there anyway to solve this ?
One way do to perform the partial select would be to change your selector expression to be of type:
Then you would call it like:
The implementation: