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
LINQ到实体-跨关系查询和筛选_Linq_Linq To Entities - Fatal编程技术网

LINQ到实体-跨关系查询和筛选

LINQ到实体-跨关系查询和筛选,linq,linq-to-entities,Linq,Linq To Entities,我很难用LINQ和Lambda表达式查询一组相关实体 我有四个相关的实体 车辆1:n车辆类型n:1价格1:n客户类型 我正在尝试获取给定车辆和客户类型的价格列表。例如,我想获得福特野马(VehicleTypeId=2)的所有价格。在这些价格中,我希望包括与价格相关的客户类型(政府、商业、零售) 我想我可以做到以下几点 Prices.Include(p => p.VehicleTypes) .Include(p => p.CustomerTypes) .Wher

我很难用LINQ和Lambda表达式查询一组相关实体

我有四个相关的实体

车辆1:n车辆类型n:1价格1:n客户类型

我正在尝试获取给定车辆和客户类型的价格列表。例如,我想获得福特野马(VehicleTypeId=2)的所有价格。在这些价格中,我希望包括与价格相关的客户类型(政府、商业、零售)

我想我可以做到以下几点

Prices.Include(p => p.VehicleTypes)
      .Include(p => p.CustomerTypes)
      .Where(p => p.VehicleTypes.Vehicles.Select(v => v.Id == 2)
但是我得到了这个错误

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<bool>' to 'bool'
无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“bool”
我似乎无法创建一个Where条件,在该条件下,我可以过滤待购买车辆的Id,但还可以在结果中包含CustomerType


编辑:只是想注意,我已经使用System.Data.Entity包含了
,因此我可以访问类型安全包含扩展

如果您需要该类型车辆和特定客户类型的价格,您可以按如下方式进行筛选:

var prices= Prices.Include(p => p.VehicleTypes)
                  .Include(p => p.CustomerTypes)
                  .Where(p => p.VehicleTypes.Vehicles.Any(v => v.Id == 2)// With this condition you make sure that Mustang belong to this set of vehicles
                           && p.CustomerTypes.Type=="Commercial");
但如果要过滤结果中的车辆,则需要将查询投影到匿名类型或DTO:

var query=  Prices.Include(p => p.VehicleTypes)
                  .Include(p => p.CustomerTypes)
                  .Where(p => p.VehicleTypes.Vehicles.Any(v => v.Id == 2)
                           && p.CustomerTypes.Type=="Commercial")
                  .Select(p=>new {CustomerType=p.CustomerTypes.Type, 
                                  Vehicles=p.VehicleTypes.Vehicles.Where(v => v.Id == 2)});

非常感谢你!现在我看到了一个格式正确的示例,这很有意义。因此,如果我需要在查询结果中包含车辆,那么我需要使用投影并再次按车辆ID进行过滤?上一个筛选器不适用?欢迎使用@webworm;)。是的,毫无疑问,正如我在上面的选择中所示,您需要进行项目,第一次只是筛选您的
价格
实体