【问题标题】:perform inner Join for 3 tables in EF对 EF 中的 3 个表执行内部联接
【发布时间】:2016-10-18 10:11:37
【问题描述】:

您好,我正在尝试为三个表实现内部联接 我从两张表开始,它给了我适当的结果。

但我不知道如何实现三个表的连接? 有人可以帮忙吗?

这是我想在 EF 中执行的选择语句:

SELECT
  capt_ar AS VehicleState,
  cont_name,
  vehl_drivername,
  vehl_name,
  vehl_entrancedate,
  vehl_customsdec,
  cont_rampid
FROM Container
  INNER JOIN
  Vehicle ON Container.cont_vehicleid = Vehicle.vehl_VehicleID
  INNER JOIN
  Custom_Captions ON Vehicle.vehl_state = Custom_Captions.Capt_Code
WHERE capt_family = 'vehl_state'
      AND vehl_Deleted IS NULL AND cont_Deleted IS NULL
      AND vehl_ClearanceCompany = 471

更新:SQL server 中的查询结果

更新:这是我尝试做的,但没有输出

var result = (from cont in db.Containers
              join veh in db.Vehicles on cont.cont_vehicleid equals veh.vehl_VehicleID
              join cap in db.Custom_Captions on veh.vehl_state equals cap.Capt_Code
              where cap.Capt_Family == "vehl_state && veh.vehl_Deleted == null && cont.cont_Deleted == null && veh.vehl_ClearanceCompany =="+p.pusr_CompanyId
              select new { cap.Capt_AR, cont.cont_Name, veh.vehl_drivername, veh.vehl_Name, veh.vehl_entrancedate, veh.vehl_customsdec, cont.cont_rampid }

【问题讨论】:

  • 在你已经声称已经编码的两个中添加另一个 JOIN
  • 你能展示一些示例数据和预期结果吗?
  • 如果你没有告诉我们关于你想加入的另一个表的有用信息,至少就像它的外键一样,那么你将不得不自己做
  • 我要对三个表进行内连接:Container,Vehicle,Custom_Captions 这是三个表
  • 你停止了,还是查询失败了?如果你停止了,那么继续,运行它,看看生成了什么,一个表或一个错误

标签: c# mysql asp.net entity-framework


【解决方案1】:
var query = (from con in db.Containers
                             join v in db.Vehicles on con.cont_vehicleid equals v.vehl_VehicleID
                             join cust in db.Custom_Captions on v.vehl_state equals cust.Capt_Code
                             where cust.Capt_Family== "vehl_state" && v.vehl_Deleted==null && con.cont_Deleted==null && v.vehl_ClearanceCompany==p.pusr_CompanyId
                             select new
                             {
                                 cont_name=con.cont_Name,
                                 vehl_Name=v.vehl_Name,
                                 VehicleState=v.vehl_state,
                                 vehl_drivername=v.vehl_drivername,
                                 vehl_entrancedate=v.vehl_entrancedate,
                                 vehl_customsdec=v.vehl_customsdec,
                                 cont_rampid=v.vehl_rampid
                             }
                             ).ToList();

【讨论】:

    猜你喜欢
    • 2016-07-29
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多