开发者

OrderBy with Dynamic Linq and one to many relationship in EF

开发者 https://www.devze.com 2023-02-06 00:06 出处:网络
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

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.


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消