gpt4 book ai didi

c# - 澄清两列同名

转载 作者:行者123 更新时间:2023-11-29 04:42:46 25 4
gpt4 key购买 nike

我有这个问题:

SELECT CableNumber, CblType, ApprxmtLngth, ME.EquipmentNumber, ME2.EquipmentNumber, CblStatus, InstallMthd, S.PrdtCd FROM CableId C
INNER JOIN MajorEquipment ME ON C.FromLoc = ME.MEId
INNER JOIN MajorEquipment ME2 ON C.ToLoc = ME2.MEId
INNER JOIN SupplierInfo S ON C.SupplierId = S.SupplierId WHERE
(@CblType IS NULL OR CblType LIKE @CblType)
AND (@CblStatus IS NULL OR CblStatus LIKE @CblStatus)
AND (@FromLoc IS NULL OR ME.EquipmentNumber LIKE @FromLoc)
AND (@ToLoc IS NULL OR ME2.EquipmentNumber LIKE @ToLoc);

当我将结果传递到模型中时,如何定义哪个 EquipmentNumber 进入哪个变量?

while (mySqlReader.Read())
{
CableID_Controller.CList.Add(new CableID_Model
{
CableNumber = Convert.ToString(mySqlReader["CableNumber"]),
CableType = Convert.ToString(mySqlReader["CblType"]),
SupplierPart = Convert.ToString(mySqlReader["PrdtCd"]),
CableLength = Convert.ToInt32(mySqlReader["ApprxmtLngth"]),
InstallMethod = Convert.ToString(mySqlReader["InstallMthd"]),
Origin = Convert.ToString(mySqlReader["ME.EquipmentNumber"]),
Destination = Convert.ToString(mySqlReader["ME2.EquipmentNumber"]),
Status = Convert.ToString(mySqlReader["CblStatus"])
});
}

因为 ME.EquipmentNumberME2.EquipmentNumber 不是 header 的实际名称。

最佳答案

描述

只需在查询中为列指定不同的别名(EquipmentNumber1EquipmentNumber2),并使用 as 运算符进行编码。您还必须删除代码中的 ME1ME2

示例

SELECT CableNumber, CblType, ApprxmtLngth, ME.EquipmentNumber as EquipmentNumber1, ME2.EquipmentNumber as EquipmentNumber2, CblStatus, InstallMthd, S.PrdtCd FROM CableId C
INNER JOIN MajorEquipment ME ON C.FromLoc = ME.MEId
INNER JOIN MajorEquipment ME2 ON C.ToLoc = ME2.MEId
INNER JOIN SupplierInfo S ON C.SupplierId = S.SupplierId WHERE
(@CblType IS NULL OR CblType LIKE @CblType)
AND (@CblStatus IS NULL OR CblStatus LIKE @CblStatus)
AND (@FromLoc IS NULL OR ME.EquipmentNumber LIKE @FromLoc)
AND (@ToLoc IS NULL OR ME2.EquipmentNumber LIKE @ToLoc);

while (mySqlReader.Read())
{
CableID_Controller.CList.Add(new CableID_Model
{
CableNumber = Convert.ToString(mySqlReader["CableNumber"]),
CableType = Convert.ToString(mySqlReader["CblType"]),
SupplierPart = Convert.ToString(mySqlReader["PrdtCd"]),
CableLength = Convert.ToInt32(mySqlReader["ApprxmtLngth"]),
InstallMethod = Convert.ToString(mySqlReader["InstallMthd"]),
Origin = Convert.ToString(mySqlReader["EquipmentNumber1"]),
Destination = Convert.ToString(mySqlReader["EquipmentNumber2"]),
Status = Convert.ToString(mySqlReader["CblStatus"])
});
}

更多信息

关于c# - 澄清两列同名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24948366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com