2

私はlinqにかなり慣れていないので、次の要件を持つ2つのテーブルを結合する必要があります:

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

ご入力いただきありがとうございます。どの答えも私が望んでいたことを完全には実行しませんでしたが、元のコードを機能させることができました。

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 に答える
2

これは、Linqpadで C# プログラムとして機能します。

基本的に、結合構文を微調整する必要があり (これを参照)、「t2」に結合するものがない場合を考慮する必要がありました (そのため、null チェックを行い、null の場合は 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((新しいディストリビューター[] {dist1、dist2、dist3、dist4、dist5、dist6、dist7、
    dist8、dist9、dist10、dist11、dist12}));

            リスト ListOfSAPGroup = new List();
            ListOfSAPGroup.AddRange(新しい SAPGroup[] { 部門 1、部門 2、部門 3、部門 4 });

            var final = ListOfDistributors の t1 から
                        ListOfSAPGroup の t2 に参加する
                        新しい { t1.SupplierID, t1.VatRateID } が新しい { t2.SupplierID, t2.VatRateID } に等しい
                        JoinedDistAndGrouped に
                        JoinedDistAndGrouped.DefaultIfEmpty() の t2 から
                        新しいものを選択
                        {
                            Name1 = t1.名前、
                            Name2 = (t2 == null) ? 「名前なし」: t2.Name,
                            サプライヤー ID = t1.サプライヤー ID、
                            金額 = t1.Amount - (t2 == null ? 0 : t2.Amount),
                            Advance = t1.Advance - (t2 == null ? 0 : t2.Advance),
                            残高 = t1.Advance - (t2 == null ? 0 : t2.Balance),
                            VatRateID = t1.VatRateID
                        };

            final.Dump();
        }
    }


    クラスディストリビューター
    {
        パブリック文字列の名前 {get; 設定; }
        public int SupplierID { get; 設定; }
        public int VatRateID { get; 設定; }
        public int DeptSupplierID { get; 設定; }
        public int 金額 { get; 設定; }
        public int Advance {get; 設定; }
        public int Balance {get; 設定; }
    }

    クラス SAPGroup
    {
        public int SupplierID { get; 設定; }
        public int VatRateID { get; 設定; }
        パブリック文字列の名前 {get; 設定; }
        public int 金額 { get; 設定; }
        public int Advance {get; 設定; }
        public int Balance {get; 設定; }
    }

    パブリッククラスの結果
    {
        パブリック文字列 Name1 { get; 設定; }
        パブリック文字列 Name2 { get; 設定; }
        public int SupplierID { get; 設定; }
        public int 金額 { get; 設定; }
        public int Advance {get; 設定; }
        public int Balance {get; 設定; }
        public int VatRateID { get; 設定; }
    }
于 2012-05-23T16:17:46.187 に答える
0

thisによると、次のようなものを探しています (これはテストされていませんが、うまくいけば正しい軌道に乗ることができます):

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 に答える