English 中文(简体)
Linq 左侧加上多个列和默认值 [重复]
原标题:Linq left join with multiple columns and default values [duplicate]
最佳回答

谢谢你们的投入 没有一个答案能如我所愿

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
                };

如果你对此有任何评论或改进,请通知我,谢谢。

问题回答

这在“http://www.linqpad.net/” rel=“no follow”>Linqpad 中作为 C# 程序使用。

基本上,您的加入语法需要调换(见这个 ),当“t2”没有连接时,你需要考虑(因此,当无效时,我们进行无效检查和使用 0,否则t2.Amount,等等)

我创造了一些假数据 这样你就可以到处玩了