Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lambda expression-使用EntityFramework Include()和任何操作?_Lambda_Entity Framework 5 - Fatal编程技术网

Lambda expression-使用EntityFramework Include()和任何操作?

Lambda expression-使用EntityFramework Include()和任何操作?,lambda,entity-framework-5,Lambda,Entity Framework 5,下面的代码正在运行 var queryResults = _db.Projects .Include("Participants.Person") .Where(Project => Project.Participants.Any(Parti => Parti.Person.FirstName == "test3")); 我正在动态构建lambda表达式。为了实现上述目标,我必须编写大量代码 我希望实现以下目标 var queryRes

下面的代码正在运行

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 PropertyYouter=Expression.Property(c,“参与者”)

ParameterExpression tpe=Expression.Parameter(typeof(Participant),“Participant”);
expressionleft1=Expression.Property(tpe,typeof(Participant).GetProperty(“Person”);
Expression left2=Expression.Property(left1,typeof(Person).GetProperty(“FirstName”);
表达式right1=表达式常数(filter.FieldValue);
表达式InnerLambda=Expression.Equal(left2,right1);
表达式innerFunction=Expression.Lambda(InnerLambda,tpe);
MethodInfo method=typeof(Enumerable).GetMethods().Where(m=>m.Name==“Any”&&m.GetParameters().Length==2.Single().MakeGenericMethod(typeof(Participant));
MemberExpression PropertyYouter=Expression.Property(c,“参与者”);
var anyExpression=Expression.Call(方法、PropertyYouter、innerFunction);

也许这对你有用

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

我找到了解决办法,它起作用了

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);
ParameterExpression c=Expression.Parameter(typeof(T),entityType.Name);
ParameterExpression tpe=表达式.参数(类型(参与者),“参与者”);
expressionleft1=Expression.Property(tpe,typeof(Participant).GetProperty(“Person”);
Expression left2=Expression.Property(left1,typeof(Person).GetProperty(“FirstName”);
表达式right1=表达式常数(filter.FieldValue);
表达式InnerLambda=Expression.Equal(left2,right1);
表达式innerFunction=Expression.Lambda(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(“参与者”);
var anyExpression=Expression.Call(方法、外部、内部函数);

这帮了大忙

不,我没有在这行中找到子实体。选择多个(p=>p.Participants。)您是否从建议的_db.Persons或表单_db.Projects中选择了?我仅从_db.Persons中选择。确定这意味着个人和参与者处于M-N关系,对吗?请参阅编辑的建议。。。SelectMany(P= > P.参加者SelectMany(PAL= > PAR项目))
 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);