OrderBy with Dynamic Linq and one to many relationship in EF

c# dynamic-linq linq linq-to-entities linq-to-sql

Question

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.

1
0
1/19/2011 5:44:24 AM

Popular Answer

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();
}
1
6/24/2011 9:02:37 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow