Julien
Julien

Reputation: 121

OrderBy with Dynamic Linq and one to many relationship in EF

I'd like to implement a module for filtering and paging. I understand that to suceed I had to use Dynamic Linq or Reflection, so I started trying to make it work .. but since the field that contains the text to be filtered in a one to many relationship EF not like it.

This code work fine .. but is static :

List<Domain.Entities.Action> actions = db.Actions.Include("Menus").Include("ActionDetails")
                                                 .Where(x => x.ActionDetails.Any(y => y.Language.Culture == _currentCulture))
                                                 .OrderBy(y => y.ActionDetails.Select(z => z.Title).Max()).Skip((pager.Index - 1) * pager.Take).Take(pager.Take)
                                                 .ToList();

I want the

.Select(z => z.Title)

Dynamic..

Can someone help me .. I Try a lot of thing .. but no sucess

Ju.

Upvotes: 0

Views: 1508

Answers (1)

Craig Richards
Craig Richards

Reputation: 59

In order to accomplish this you need to pass in a parameter of Funt<Action, TResultType> searchCriteria

Not sure what your method signature is like but this would work if you plan on returning a List<string>

public List<string> PerformSearch(Func<Action, string> selectCriteria)
{

  return db.Actions.Include("Menus").Include("ActionDetails")
      .Where(x => x.ActionDetails.Any(y => y.Language.Culture == _currentCulture))
      .OrderBy(y => y.ActionDetails.Select(**selectCriteria**).Max())
      .Skip((pager.Index - 1) * pager.Take).Take(pager.Take)
      .ToList();
}

Upvotes: 1

Related Questions