简体   繁体   English

使用左联接和if语句的实体框架查询

[英]entity framework query using left join and if statement

I have two tables- workers and vehicles: Vehicle(Id, VehicleNumber) Workers(Id, Name, VehicleId) 我有两个表-worker和Vehicles:Vehicle(Id,VehicleNumber)Workers(Id,Name,VehicleId)

I need to write a method that get a bool? 我需要编写一个获取布尔值的方法? parameter "isFree" and uses the entity framework (prefer lambda and not linq to entities) in order to: 参数“ isFree”并使用实体框架(首选lambda而不是linq而不是实体)以:
if isFree==null then returns all vehicles 如果isFree == null,则返回所有车辆
if isFree==true then returns all vehicles that doesn't belong to any worker 如果isFree == true,则返回不属于任何工人的所有车辆
if isFree==false then returns all vehicles that belong to some worker 如果isFree == false,则返回属于某个工人的所有车辆

What is the best practise to solve this? 解决此问题的最佳做法是什么?

if(!isFree.HasValue)
  return context.Vehicles;
else if(isFree)
  return context.Vehicles.Where(v => !v.Workers.Any());
else
  return context.Vehicles.Where(v => v.Workers.Any());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM