0

I have an expression which I can directly use with EF repository:

Expression<Func<Image, bool>> exprNonPrivateMembers = 
    i => i.Member.IsPrivate == false;  

var images = imagesRepository.All().Where(exprNonPrivateMembers);

somewhere in my business logic I already have this "searchable" expression as follows:

Expression<Func<Member, bool>> memberIsSearchable = 
    m => m.IsPrivate == false;  

I want to reuse it in my new expression:

Expression<Func<Image, bool>> exprNonPrivateMembers = 
    i => memberIsSearchable(i.Member);  // pseudocode

and here is the domain model:

class Image
{
    public Member Member { get; set;}
}

class Member
{
    public bool IsPrivate { get; set;}
}

how can I do that?

note: this is very simplified piece of code, please do not suggest removing expressions etc for 2 reasons:

  1. I'm still learning expressions magic and I want to learn something new
  2. This really needs to be done using expressions :)
avs099
  • 10,937
  • 6
  • 60
  • 110
  • `imagesRepository.All().Where(...)`? How can you use `Where` after `All`? Or for that matter, how do you have `All` without a predicate? – sircodesalot Jan 06 '14 at 13:34
  • that's a standard EF code I believe. All() returns IQueryable – avs099 Jan 06 '14 at 13:41
  • I guess I'm confused: if you already have lambdas that do what you want it to do... why not just use those? I'm not clear on what value is added by expression trees? Expression trees are generally for when you want to either 'reflect over' code structure, or compile new code. I guess it would be easier to help you if you focused on the immediate problem rather that the method you're using to solve it. – sircodesalot Jan 06 '14 at 13:45
  • i guess you are missing the point of IQueryable. Read this: http://stackoverflow.com/questions/2876616/returning-ienumerablet-vs-iqueryablet - basically i want my expression to be executed on SQL Server side as opposite to the app memory – avs099 Jan 06 '14 at 13:56
  • Oh I see, I haven't used EF in a while, but you're right, it 'reflects over' expression trees to generate the relevant server-side SQL. That aside, it's still not clear what you're trying to do. – sircodesalot Jan 06 '14 at 14:05
  • trying to reuse existing expression with business logic, so I don't have to copy/paste it. – avs099 Jan 06 '14 at 14:17
  • I'm looking for a problem and not seeing it. What's not working about what you have? – sircodesalot Jan 06 '14 at 14:24
  • pls re-read the question then. The problem is described on line #3 "I want to `reuse` it in my new expression:" – avs099 Jan 06 '14 at 16:05
  • What happens when you try? (Your question is how to re-use it, and then you show code of it being re-used. What's going on?) – simon at rcl Jan 06 '14 at 17:23
  • it does not compile - i have comment there saying "pseudocode" – avs099 Jan 06 '14 at 17:24

0 Answers0