0

I'm trying to figure out how to combine the following Expressions:

Expression<Func<TParent, ICollection<TChild>>> childSelector = p => p.Children;

Expression<Func<TChild, bool>> childPredicate = c => c.NameLast.StartsWith("V");

var anyExpression = childSelector <------> .Any(childPredicate);

The "<------>" is the part I don't know what to do with. I'm using LINQKit, I just can't figure out how to combine these expressions.

Can someone give me a hand?

Dylan Vester
  • 2,686
  • 4
  • 29
  • 40

3 Answers3

1

I'm not 100% clear on what you want anyExpression to be, but this is my best guess.

Expression<Func<TParent, bool>> anyExpression = p => p.Select(childselector).Any(childPredicate);
juharr
  • 31,741
  • 4
  • 58
  • 93
  • This may work in other cases, and I did actually try this. While it did build, it gave me an error at runtime regarding the variable "p" not being defined. Which made sense, with the way I was building the predicate in other places in my code. Thanks for your answer, hopefully it will help someone here. – Dylan Vester Jun 27 '11 at 13:00
0

you can do childselector(parent).Any(childPredicate) where parent is a TParent instance.

Bas
  • 26,772
  • 8
  • 53
  • 86
  • Thanks for the response, however, I won't have an instance of a parent until much later in the program execution. I'm trying to build an expression try across multiple classes, so I'm a few levels removed from the actual querying. – Dylan Vester Jun 22 '11 at 19:22
0

I found the answer to my problem

childSelector.Combine(p => p.AsQueryable().Any<TEntity>(this.Predicate), true)

This is how the code turned out.

Community
  • 1
  • 1
Dylan Vester
  • 2,686
  • 4
  • 29
  • 40