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# C实体框架与Linq查询_C#_Entity Framework_Linq_Lambda - Fatal编程技术网

C# C实体框架与Linq查询

C# C实体框架与Linq查询,c#,entity-framework,linq,lambda,C#,Entity Framework,Linq,Lambda,在下面的查询构建中,是否有任何原因将Where子句B与Where子句A分开 它们可以直接连接到A或B中吗?例如: truckList = (from truck in _dbcontext.Truck.Where( x => x.ClientId == clientId && truck.EquipmentId > 0 && truck.FieldId

在下面的查询构建中,是否有任何原因将Where子句B与Where子句A分开

它们可以直接连接到A或B中吗?例如:

truckList = (from truck in _dbcontext.Truck.Where(
                x => x.ClientId == clientId && 
                truck.EquipmentId > 0 && 
                truck.FieldId > 0)
                ...rest of query...
             )

这是因为“truck”和“comp”来自不同的表

truckList = (from truck in _dbcontext.Truck.Where(x => x.ClientId == clientId) //(A)
join comp in _dbcontext.TruckComponent
on truck.Equipment.ProtId equals comp.ComponentId
where truck.EquipmentId > 0 && truck.FieldId > 0 //B
select NewTruckVmFromDbTruck(truck, comp)).AsEnumerable();
卡车来自一张叫做卡车的桌子,而 comp来自名为TruckComponent的表

两者都有不同的标识,如果只选择“Truck”表,将无法获得“TruckComponent”表数据

select NewTruckVmFromDbTruck(truck)).AsEnumerable();

希望这有帮助

无论如何,最好不要混合使用fluent和lambda查询语法,怎么样:

truckList = (from truck in _dbcontext.Truck
             where truck.EquipmentId > 0 && truck.FieldId > 0 && truck.ClientId == clientId
             join comp in _dbcontext.TruckComponent on truck.Equipment.ProtId equals comp.ComponentId
             select NewTruckVmFromDbTruck(truck, comp)).AsEnumerable();

当您运行SQL跟踪时,两个LINQ查询是否生成了相同的数据库查询?Where子句B不依赖于comp的任何内容,因此不需要与AB分开,AB实际上不是与TruckComponent分开的。所以你在这一点上错了。B是独立的,适用于两个表。要引用TruckComponent,请使用join语句OWH中定义的comp,如果是这样,则可以使用任何一种方式。我遗漏了单词“clause”。从truck中的_dbcontext.truck.Wherex=>x.ClientId==ClientId&&x.EquipmentId>0&&x.FieldId>0可以编辑答案以显示此内容。A和B来自不同的表格部分,这会误导别人,或者让你否定这个答案。我明白了,这是一个有点棘手的问题,也许下次我在回答时应该更加小心,因为你指出了我犯的错误,而不是希望找到你问题的答案。抱歉犯了愚蠢的错误,不会再发生了
truckList = (from truck in _dbcontext.Truck
             where truck.EquipmentId > 0 && truck.FieldId > 0 && truck.ClientId == clientId
             join comp in _dbcontext.TruckComponent on truck.Equipment.ProtId equals comp.ComponentId
             select NewTruckVmFromDbTruck(truck, comp)).AsEnumerable();