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
C# 带LINQ的表达式过滤器单个实体_C#_Entity Framework_Linq - Fatal编程技术网

C# 带LINQ的表达式过滤器单个实体

C# 带LINQ的表达式过滤器单个实体,c#,entity-framework,linq,C#,Entity Framework,Linq,我有一个实体,我们称之为汽车,我为它创建了一个过滤器 public static Expression<Func<Car, bool>> FilterCars(int something) { ...} publicstaticexpressionfiltercars(int-something){…} 当我像这样查询数据库时,它会起作用: Repository.GetAll<Car>().Where(FilterCars(1)); Reposit

我有一个实体,我们称之为汽车,我为它创建了一个过滤器

   public static Expression<Func<Car, bool>> FilterCars(int something) { ...}
publicstaticexpressionfiltercars(int-something){…}
当我像这样查询数据库时,它会起作用:

Repository.GetAll<Car>().Where(FilterCars(1));
Repository.GetAll<Person>().Where(p => p.Car.Matches(FilterCars(1));
Repository.GetAll().Where(FilterCars(1));
问题是我也有一对一的关系,我想重用这个过滤器

假设一个人只有一辆车,我只想要拥有与我的过滤器匹配的车的人,就像这样:

Repository.GetAll<Car>().Where(FilterCars(1));
Repository.GetAll<Person>().Where(p => p.Car.Matches(FilterCars(1));
Repository.GetAll().Where(p=>p.Car.Matches(FilterCars(1));
我发明了matches方法,但这是我正在寻找的东西


有人知道在数据库查询中“过滤单个对象”的方法吗?

有人知道…?EF不支持自定义方法(如您发明的)在表达式树中。如果你想重用表达式,你需要一些表达式合成帮助程序。你可以查看包。或者查看SO上发布的一些表达式合成帮助程序。例如,你可以将我的答案中的帮助程序带到并像
.Where(FilterCars(1.ApplyTo)((Person p)=>p.Car)一样使用它
您的助手工作得非常出色!谢谢:)将其作为answer@Moddaman很高兴它帮助了你:)但是这里不允许发布重复的答案,所以让它保持原样吧。