代码之家  ›  专栏  ›  技术社区  ›  Ramesh

十进制的?with Left outer join在LINQ中获取空引用

  •  0
  • Ramesh  · 技术社区  · 6 年前

    我想在林肯做两个左外联 var 但是在选择所需的颜色时,我得到 Object reference not set to an instance of an object 在我想要的地方出错 Nullable decimal .

        var FLS = (from ee in SumTillFYEnd 
                            join es in SumTillFYStart on ee.Account equals es.Account into temp
                            from t in temp.DefaultIfEmpty()                             
                            select new
                            {  
                                Account = ee.Account, // As of here it works
                                BeginDr = (t.DrStartCF == 0) ? (decimal?) null : t.DrStartCF // Here I get error Object reference not set to an instance of an object.
                            });
    

    有时SumTillFYEnd,有时sumtillfyest变为空。我想加入应该使用默认值,以防任何一个或两者都为空。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Chris Pratt    6 年前

    问题是试图转换 null decimal? . 你不能直接投 无效的 到另一个类型,是否可以为空。这总是会导致 NullReferenceException . 你想要的是 default . 换言之,替换:

    (decimal?)null
    

    default(decimal?)
    
        2
  •  0
  •   hiFI    5 年前

    我用默认类解决了这个问题。

    我看到的原因是decimal不能为空,所以它也需要为默认值设置 0 decimal.MinValue

    所以,您需要为 SumTillFYStart 喜欢

    var defaultSumTillFYStart = new SumTillFYStart { Account = string.Empty, DrStartCF =0};

    在上面的上下文中,然后在您的代码片段中替换

    from t in temp.DefaultIfEmpty()

    带着这个

    from t in temp.DefaultIfEmpty(defaultSumTillFYStart)

    我有一个linqPad,写在下面,但为不同的子集;我认为它将帮助某些人:

    void Main()
    {
        List<Debtor> debtors = new List<Debtor>();
        List<SecurityHolding> holdings = new List<SecurityHolding>();
    
        //Initialize Debtor
        debtors.Add(new Debtor(){
            AccountId = "J1",
            OutstandingValue = 501.95M
        });
    
        debtors.Add(new Debtor(){
            AccountId = "J2",
            OutstandingValue = 75.68M
        });
    
        debtors.Add(new Debtor(){
            AccountId = "J3",
            OutstandingValue = 100.01M
        });
    
        //Initialize Security Holding
        holdings.Add(new SecurityHolding(){
            AccountId = "J2",
            SecurityHoldingValue = 100M
        });
    
        holdings.Add(new SecurityHolding(){
            AccountId = "J3",
            SecurityHoldingValue = 200M
        });
    
    
        var defaultHolding = new SecurityHolding { AccountId= string.Empty, SecurityHoldingValue = 0};
    
        var result = (from d in debtors
                       join p in holdings
                       on d.AccountId equals p.AccountId into temp
                       from t in temp.DefaultIfEmpty(defaultHolding)
                       select new
                       {
                            AccountId = d.AccountId,
                            OutstandingValue = d.OutstandingValue,
                            HoldingValue = (decimal?)t.SecurityHoldingValue
                       });
    
        result.Dump();
    }
    
    // Define other methods and classes here
    public class Debtor
    {
        public string AccountId {get;set;}
        public decimal OutstandingValue {get;set;}
    }
    
    public class SecurityHolding
    {
        public string AccountId {get;set;}
        public decimal SecurityHoldingValue {get;set;}
    }
    

    这里是输出:

    enter image description here

    推荐文章