尝试加入表时出现c#SQL Query错误

时间:2017-10-16 12:32:43

标签: c# sql

我在我的代码中加入表格时遇到了困难。下面你可以看到我的代码,我在连接2表上试图连接我的零件表时出错。

if (query.Any()) // Check if REG is in the Database
{
    int carID = query.FirstOrDefault().Id;
    string carRegg = query.FirstOrDefault().regNo;
    string carMake = query.FirstOrDefault().Make;
    string carModel = query.FirstOrDefault().Model;


    var test = (from a in dbC.Cars
                where a.Id == carID
                join b in dbC.Services on a.Id equals b.ServiceWrkNo
                join c in dbC.PartsUseds on b.ServiceWrkNo equals c.PartsUsedNo
                join d in dbC.Parts on c.PartsUsedNo equals d.PartName
                select new
                {
                    serviceNum = b.ServiceWrkNo,
                    PartNo = c.PartsUsedNo,
                    replacedParts = d.PartName
                }).ToList();

我从模型第一种方法创建的数据库如下。

enter image description here

1 个答案:

答案 0 :(得分:1)

您的加入似乎有点偏离 - 您可以尝试使用以下列加入

var test = (from a in dbC.Cars
            where a.Id == carID
            join b in dbC.Services on a.Id equals b.CarId
            join c in dbC.PartsUseds on b.ServiceWrkNo equals c.ServiceServiceWrkNo
            join d in dbC.Parts on c.PartsPartNo equals d.PartNo
            select new
            {
                serviceNum = b.ServiceWrkNo,
                PartNo = c.PartsUsedNo,
                replacedParts = d.PartName
            }).ToList();