2012-11-02 51 views
0

下面的代碼工作Lambda Expresssion - 使用EntityFramework包含()和任何操作?

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

我動態構建lambda表達式。爲了實現上述,我必須編寫大量的代碼。

我想實現以下目標。

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

任何建議請分享。

編輯區段以下

我與任何操作嘗試。但我在這一行中得到了例外。有什麼建議麼?

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); 

回答

0

我得到了解決,其工作。

Lambda表達式

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

的源代碼來構建動態lambda表達式

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); 

這幫助很大。 Building a dynamic expression tree to filter on a collection property

0

也許這是對你有用。

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

編輯:或者,如果人有很多參與者

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

不,我找不到這個行的子實體.SelectMany(p => p.Participants。) – sivaL

+0

您是否從_db.Persons中選擇了建議的或形式爲_db.Projects? –

+0

我只從_db.Persons中選擇。 – sivaL