1

我有两个对象,一个从 csv ( fileAuto,first) 创建,另一个从 datatable( dbAuto,second) 创建。两者都有count专栏。

我想在多列和结果中加入这两者,以包含第二个与第一个不匹配的项目,还包含该count列和第一个并将其添加到列表中。

我以为我想使用.Except(),但我不知道如何有一个比较器来比较多列。我认为 join 会起作用,但我收到一个错误,提示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) 
                };
}
4

1 回答 1

3

你不需要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
    };
于 2013-03-30T17:38:37.377 回答