我有两个对象,一个是从csv(fileAuto,first
)创建的,另一个是从数据表(dbAuto,second
)创建的。两者都有count
列。
我想在多个列和结果中加入两个,以包含第二个与第一个列不匹配的项,并且还包含count
列,并从第一个列添加到列表中。
我以为我想使用.Except()
,但我不知道如何让比较器来比较多列。我认为连接可以工作,但我收到一个错误,说 join子句中的一个表达式不正确。
我对Linq的理解很疏忽,所以我不太清楚如何实现这个目标。我已经包含了后者的代码。
public class FileAuto
{
public string DealerCode;
public string ModelCode;
public int ModelYear;
public string ExteriorCodeCode;
public string InteriorColorCode;
public int FileVehicleCount;
}
public class DbAuto
{
public string DealerCode;
public string DealershipName;
public string ModelCode;
public int ModelYear;
public string ExteriorCodeCode;
public string InteriorColorCode;
public int DbVehicleCount;
}
public class IDiscObj // INVENTORY DISCREPANCIES object
{
public string DealerCode;
public string DealershipName;
public string ModelCode;
public int ModelYear;
public string ExteriorCodeCode;
public string InteriorColorCode;
public int FileVehicleCount;
public int DbVehicleCount;
}
var autoscombined =
from f in fileInv
join d in dbInv
on
new {f.DealerCode, f.ModelCode, f.ModelYear, f.ExteriorCodeCode, f.InteriorColorCode}
equals
new {d.DealerCode, d.ModelCode, d.ModelYear, d.ExteriorCodeCode, d.InteriorColorCode}
select (a => new IDiscObj {
DealerCode = f.DealerCode,
DealershipName = d.DealershipName,
ModelCode = f.ModelCode,
ModelYear = f.ModelYear,
ExteriorCodeCode = f.ExteriorCodeCode,
InteriorColorCode = f.InteriorColorCode,
FileVehicleCount = f.FileVehicleCount,
DbVehicleCount = d.DbVehicleCount
});
我也希望得到这些结果,并将按照我在下面列出的经销商代码分组的车辆数量相加。如果有人能告诉我我是否正确地选择了这些解决方案,或者采用更合适的方式实现这一目标会非常棒。
if (DDOlst.Count > 0) //list of IDiscObj
{
var ddos =
from d in DDOlst
group d by new { d.DealerCode, d.DealershipName } into g
select
new {
g.Key.DealerCode,
g.Key.DealershipName,
filecnt = g.Sum(d => d.FileVehicleCount),
dbcnt = g.Sum(d => d.DbVehicleCount)
};
}
答案 0 :(得分:3)
您的select子句中不需要a=>
:
var autoscombined = from f in fileInv
join d in dbInv
on new {f.DealerCode, f.ModelCode, f.ModelYear, f.ExteriorCodeCode, f.InteriorColorCode}
equals new {d.DealerCode, d.ModelCode, d.ModelYear, d.ExteriorCodeCode, d.InteriorColorCode}
select new IDiscObj {
DealerCode = f.DealerCode,
DealershipName = d.DealershipName,
ModelCode = f.ModelCode,
ModelYear = f.ModelYear,
ExteriorCodeCode = f.ExteriorCodeCode,
InteriorColorCode = f.InteriorColorCode,
FileVehicleCount = f.FileVehicleCount,
DbVehicleCount = d.DbVehicleCount
};