Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
子实体上的Linq表达式_Linq_Entity Framework - Fatal编程技术网

子实体上的Linq表达式

子实体上的Linq表达式,linq,entity-framework,Linq,Entity Framework,我正在构建动态linq表达式,它可以很好地用于单个实体。 例如: 我有一个叫做Employee和empeduinfo的类 public class Employee { public int Id { get; set; } public string Name { get; set; } } public class EmpEduInfo { public int Id { get; set; } public string Name { get; set;

我正在构建动态linq表达式,它可以很好地用于单个实体。 例如: 我有一个叫做Employee和empeduinfo的类

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class EmpEduInfo
{
    public int Id { get; set; }
    public string Name  { get; set; }
    public int EmpId { get; set; }
}
我需要得到所有的员工,empeduinfo类以“x”开头

我为startswith(“x”)准备了表达方式

在这种情况下,它只过滤父表,不过滤子表

我需要准备泛型表达式,以便动态过滤父对象和子对象

如果不使用表达式,我知道一个解决方案:

var temp= (from ee in entities.Employee.Include("EmpEduInfo").Where(x => x.name.StartsWith("t"))                           
           where ee.EmpEduInfo.Where(x => x.name.StartsWith("t")).Count()>0                                
           select ee).ToList();
使用表达式,我正在构建通用表达式,以提供动态的高级搜索,而不是在每个实体中编写

下面是我的表情细节

            // Get the method information for the String.StartsWith() method                
            MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
            // Build the parameter for the expression
            ParameterExpression  empparam= Expression.Parameter(typeof(employee), "ename");;
            // Build the member that was specified for the expression
            MemberExpression field = Expression.PropertyOrField(empparam, "name");
            // Call the String.StartsWith() method on the member
            MethodCallExpression startsWith = Expression.Call(field, mi, Expression.Constant("t"));                  
            var namelamda = Expression.Lambda<Func<employee, bool>>(startsWith, new ParameterExpression[] { empparam });
            var temp = entities.employees.Include("empedudetails").Where(namelamda).ToList();
//获取String.StartsWith()方法的方法信息
MethodInfo mi=typeof(string).GetMethod(“StartsWith”,新类型[]{typeof(string)});
//为表达式生成参数
ParameterExpression empparam=Expression.Parameter(typeof(employee),“ename”);;
//生成为表达式指定的成员
MemberExpression字段=Expression.PropertyOrField(empparam,“名称”);
//对成员调用String.StartsWith()方法
MethodCallExpression startsWith=Expression.Call(字段,mi,Expression.Constant(“t”));
var namelamda=Expression.Lambda(startsWith,新参数Expression[]{empparam});
var temp=entities.employees.Include(“empedudetails”).Where(namelamda.ToList();

您可以查看编译器使用
IQueryable:

IQueryable<Employee> query = 
  from ee in entities.Employee ...

var expression = query.Expression;

我试着在EF4.0中,或者我们为同一版本编写了DB扩展

EF 4.1中提供了选项


谢谢。

您可能忘记了
EmpEduInfo
集合在
Employee
类中。“泛型表达式”是指可以应用于任何类的表达式?你能给我看一下表达的代码吗?谢谢你的回复。我在botton的加载项代码中的表达式,我在Include pelase中包含empeduinfo检查上述代码谢谢回复。我在botton的加载项代码中的表达式,以及我在Include pelase中包含的empeduinfo检查上述代码。例如,我想搜索以x开头的所有实体名称。示例雇员是Tony、greg和EdudeDetails,分别是第十名和第七名,现在希望使用“t”获取,这意味着我需要获取Tony和Tenth。使用此查询进行测试IQueryable query=来自entities.Employee.Include(“EmpEduInfo”),其中ee.name.StartsWith(“t”)和&ee.EmpEduInfo.Any(x=>x.name.StartsWith(“t”))选择ee;它仍然没有过滤子对象的数据。您的意思是要获取所有姓名以“T”开头的
EmpEduInfo
员工,并且只包括那些以“T”开头的相关
EmpEduInfo
记录吗?这与在代码中生成
表达式
是不同的问题。在EF中,您必须使用
Select
子句来执行此操作。我建议您针对该问题开始一个新问题。或谷歌“ef过滤器导航属性”=>或我的答案如下:
IQueryable<Employee> query = 
  from ee in entities.Employee ...

var expression = query.Expression;
IQueryable<Employee> query = 
  from ee in entities.Employee.Include("EmpEduInfo")                           
  where
    ee.name.StartsWith("t") &&
    ee.EmpEduInfo.Any(x => x.name.StartsWith("t"))                             
  select ee;