2

我对 linq 相当陌生,我需要加入两个具有以下要求的表:

应该离开加入 t1 和 t2。

如果 t2 为空,则查询不应失败 - 应使用默认值。

我的查询:

var final = from t1 in saDist.AsEnumerable()
            from t2 in sapGrouped.AsEnumerable()
            where
                t1.Supplier.Id == t2.Supplier.Id && t1.VatRate == t2.VatRate
            select
                new
                {
                    t1.Supplier,
                    Amount = t1.Amount - t2.Amount,
                    Advance = t1.Advance - t2.Advance,
                    Balance = t1.Balance - t2.Balance,
                    t1.VatRate
                };

有人可以纠正这个吗?

4

3 回答 3

2

这在Linqpad中作为 C# 程序工作。

基本上你的连接语法需要调整(见这个),你需要考虑到什么时候没有什么可以加入到“t2”(所以我们做一个空检查并在空时使用 0,否则 t2.Amount 等)

我创建了一些虚拟数据,以便您可以玩耍。

有关另一个示例,请参见http://codingsense.wordpress.com/2009/03/08/left-join-right-join-using-linq/

我希望它做你想做的事。

谢谢, 多米尼克

    公开课A
    {
        无效的主要()
        {

            Distributor dist1 = new Distributor() { SupplierID = 1, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "A", DeptSupplierID = 1 };
            Distributor dist2 = new Distributor() { SupplierID = 2, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "B", DeptSupplierID = 1 };
            Distributor dist3 = new Distributor() { SupplierID = 3, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "C", DeptSupplierID = 1 };
            Distributor dist4 = new Distributor() { SupplierID = 4, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "D", DeptSupplierID = 2 };
            Distributor dist5 = new Distributor() { SupplierID = 5, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "E", DeptSupplierID = 2 };
            Distributor dist6 = new Distributor() { SupplierID = 6, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "F", DeptSupplierID = 2 };
            Distributor dist7 = new Distributor() { SupplierID = 7, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "G", DeptSupplierID = 6 };
            Distributor dist8 = new Distributor() { SupplierID = 8, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "H", DeptSupplierID = 3 };
            Distributor dist9 = new Distributor() { SupplierID = 9, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "I", DeptSupplierID = 3 };
            Distributor dist10 = new Distributor() { SupplierID = 10, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "J", DeptSupplierID = 7 };
            Distributor dist11 = new Distributor() { SupplierID = 11, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "K", DeptSupplierID = 7 };
            Distributor dist12 = new Distributor() { SupplierID = 12, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "L", DeptSupplierID = 5 };

            SAPGroup Dept1 = new SAPGroup() { SupplierID = 1, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "Development" };
            SAPGroup Dept2 = new SAPGroup() { SupplierID = 2, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "Testing" };
            SAPGroup Dept3 = new SAPGroup() { SupplierID = 3, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "Marketing" };
            SAPGroup Dept4 = new SAPGroup() { SupplierID = 4, Amount = 3, Balance = 4, Advance = 3, VatRateID = 1, Name = "Support" };

            列表 ListOfDistributors = new List();
            ListOfDistributors.AddRange((new Distributor[] { dist1, dist2, dist3, dist4, dist5, dist6, dist7,
    dist8, dist9, dist10, dist11, dist12 }));

            列表 ListOfSAPGroup = new List();
            ListOfSAPGroup.AddRange(new SAPGroup[] { Dept1, Dept2, Dept3, Dept4 });

            var final = 来自 ListOfDistributors 中的 t1
                        在 ListOfSAPGroup 中加入 t2
                        在新的 { t1.SupplierID, t1.VatRateID } 上等于新的 { t2.SupplierID, t2.VatRateID }
                        进入 JoinedDistAndGrouped
                        来自 JoinedDistAndGrouped.DefaultIfEmpty() 中的 t2
                        选择新的
                        {
                            Name1 = t1.Name,
                            Name2 = (t2 == null) ? “没有名字”:t2.Name,
                            供应商 ID = t1.供应商 ID,
                            Amount = t1.Amount - (t2 == null ? 0 : t2.Amount),
                            Advance = t1.Advance - (t2 == null ? 0 : t2.Advance),
                            余额 = t1.Advance - (t2 == null ? 0 : t2.Balance),
                            增值税率ID = t1.增值税率ID
                        };

            final.转储();
        }
    }


    类分销商
    {
        公共字符串名称 { 获取;放; }
        公共 int 供应商 ID { 获取;放; }
        公共 int VatRateID { 获取;放; }
        公共 int DeptSupplierID { 获取;放; }
        公共int金额{获取;放; }
        公共 int Advance { 获取;放; }
        公共 int 余额 { 获取;放; }
    }

    SAPGroup 类
    {
        公共 int 供应商 ID { 获取;放; }
        公共 int VatRateID { 获取;放; }
        公共字符串名称 { 获取;放; }
        公共int金额{获取;放; }
        公共 int Advance { 获取;放; }
        公共 int 余额 { 获取;放; }
    }

    公开课结果
    {
        公共字符串 Name1 { 获取;放; }
        公共字符串 Name2 { 获取;放; }
        公共 int 供应商 ID { 获取;放; }
        公共int金额{获取;放; }
        公共 int Advance { 获取;放; }
        公共 int 余额 { 获取;放; }
        公共 int VatRateID { 获取;放; }
    }
于 2012-05-23T16:17:46.187 回答
2

感谢您的输入。没有一个答案完全符合我的要求,但我设法让我的原始代码正常工作:

var final = from t2 in saDist.AsEnumerable()
            from t1 in sapGrouped.AsEnumerable().DefaultIfEmpty()
            where
                t1 == null || (t2.Supplier.Id == t1.Supplier.Id && t2.VatRate == t1.VatRate)
            select
                new
                {
                    t2.Supplier,
                    Amount = t2.Amount - (t1 == null ? 0 : t1.Amount),
                    Advance = t2.Advance - (t1 == null ? 0 : t1.Advance),
                    Balance = t2.Balance - (t1 == null ? 0 : t1.Balance),
                    t2.VatRate
                };

如果您对此有任何意见或改进,请告诉我,谢谢。

于 2012-05-24T08:42:14.770 回答
0

据此,您正在寻找类似的东西(这是未经测试的,但希望能引导您走上正确的道路)

var final = from t1 in saDist.AsEnumerable()
            join t2 in sapGrouped.AsEnumerable()
            on t1.Supplier.Id equals t2.Supplier.Id 
            and t1.VatRate equals t2.VatRate into t1_t2 //not sure about this line
            from t2 in t1_t2.DefaultIfEmpty()
            {
                t1.Supplier,
                Amount = t1.Amount - t2.Amount,
                Advance = t1.Advance - t2.Advance,
                Balance = t1.Balance - t2.Balance,
                t1.VatRate
            };

注意.DefaultIfEmpty(),这满足:“如果 t2 为空,则查询不应失败 - 应使用默认值。”

于 2012-05-23T15:36:33.347 回答