Home > Software engineering >  how to to SUM specific column including the current data using LINQ
how to to SUM specific column including the current data using LINQ

Time:10-20

I have a query it should first add up the amount in the database starting from 3 months ago until the current date,and if its more than a specific amount which i put in the condition,it should return false.

              public Task<bool> SecurityCheck(CustomerData cust)
                {
                   var checkRsult = (from x in dbContext.CustomerModel
                    where x.CustomerReference == cust.CustomerReference
                    && x.Created >= DateTime.Today.AddMonths(-3)
                    select new
                    {
                       AccomulateAmount = x.AmountToTransfer
                    }).Sum(x => x.AccomulateAmount);
                    
                    }
                    
                    var finalResult=checkRsult cust.Amount;
                    
                    if(finalResult>250000){
                    
                    //return false
                    
                    }       
                    else{
                    
                    //store th model in the db      
                    }

first of all im not sure if the way i query is right or not(the LINQ part),my second question is ,is there any way to sum all including the current incoming one(cust.amount)inside a single query? Rather than get the database sum first and then add the current one to it?

CodePudding user response:

It's slightly long winded, you could make it

dbContext.CustomerModel
    .Where(cm => cm.CustomerReference == cust.CustomerReference && cm.Created >= DateTime.Today.AddMonths(-3))
    .Sum(cm => cm.AmountToTransfer)

                
  • Related