0

The following code is working

var queryResults = _db.Projects
           .Include("Participants.Person")
           .Where(Project => Project.Participants.Any(Parti => Parti.Person.FirstName == "test3"));

I am dynamically building lambda expression. In order to achieve the above I have to write lots of code.

I would like to achieve the following.

var queryResults = _db.Projects
           .Include("Participants.Person")
           .Where(Project => Project.Participants.Person.FirstName == "test3"));

Any suggestion please share.

Edited section following

I am trying with Any operation. But i m getting exception in this line. Any suggestions?

MemberExpression propertyOuter = Expression.Property(c, "Participant");

ParameterExpression tpe = Expression.Parameter(typeof(Participant), "Participant");
Expression left1 = Expression.Property(tpe, typeof(Participant).GetProperty("Person"));
Expression left2 = Expression.Property(left1, typeof(Person).GetProperty("FirstName"));
Expression right1 = Expression.Constant(filter.FieldValue);
Expression InnerLambda = Expression.Equal(left2, right1);
Expression<Func<Participant, bool>> innerFunction = Expression.Lambda<Func<Participant, bool>>(InnerLambda, tpe);

MethodInfo method = typeof(Enumerable).GetMethods().Where(m => m.Name == "Any" && m.GetParameters().Length == 2).Single().MakeGenericMethod(typeof(Participant));

MemberExpression propertyOuter = Expression.Property(c, "Participant");

var anyExpression = Expression.Call(method, propertyOuter, innerFunction);
sivaL
  • 1,812
  • 5
  • 21
  • 30

2 Answers2

0

Perhaps this is working for you.

var queryResults = _db.Persons
   .Where( p => p.FirstName == "test3")
   .SelectMany(p => p.Participant.Projects)
   .Include("Participants.Person"); 

Edit: or if person has many participants

var queryResults = _db.Persons
   .Where( p => p.FirstName == "test3")
   .SelectMany(p => p.Participants.SelectMany(par => par.Projects))
   .Include("Participants.Person"); 
Yann Olaf
  • 597
  • 3
  • 12
0

I got the solution, its working.

Lambda expression

var queryResults = _db.Projects
       .Include("Participants.Person")
       .Where(Project => Project.Participants.Any(Participant => Participant.Person.FirstName == "test3"));

Source code to build dynamic lambda expression

 ParameterExpression c = Expression.Parameter(typeof(T), entityType.Name);
 ParameterExpression tpe = Expression.Parameter(typeof(Participant), "Participant");
 Expression left1 = Expression.Property(tpe, typeof(Participant).GetProperty("Person"));
 Expression left2 = Expression.Property(left1, typeof(Person).GetProperty("FirstName"));
 Expression right1 = Expression.Constant(filter.FieldValue);
 Expression InnerLambda = Expression.Equal(left2, right1);
 Expression<Func<Participant, bool>> innerFunction = Expression.Lambda<Func<Participant, bool>>(InnerLambda, tpe);

 MethodInfo method = typeof(Enumerable).GetMethods().Where(m => m.Name == "Any" && m.GetParameters().Length == 2).Single().MakeGenericMethod(typeof(Participant));

 var outer = Expression.Property(c, typeof(Project).GetProperty("Participants"));

 var anyExpression = Expression.Call(method, outer, innerFunction);

This helped lot. Building a dynamic expression tree to filter on a collection property

Community
  • 1
  • 1
sivaL
  • 1,812
  • 5
  • 21
  • 30