Solved

Linq Sum Query

Posted on 2014-03-03
3
571 Views
Last Modified: 2016-02-10
New to linq. Basically I need to write a query  that does 2 sums using entity framework and returns a list.  (A sum of fields from TableA and a Sum of fields from TableB) The fields will be decimals.

Also, I need to handle for the fact that there may not be a row that meets the criteria. (where claimid=claimid and unitid=unitid) . Meaning TableA could bring back 0 rows or TableB could bring back 0 rows and therefore there is nothing to sum. So in this case the result would be 0.

Below is what I have but for some reason it does not seem to bring the correct total back for Table B. Any and all advice or help would be most appreciated.

Jason

        private List<SubroRcvdEntryTotals> GetSubroEntryTotals(int currentClaim)
        {
            return (from a in _subContext.HeUnits
                    where a.Claim == currentClaim
                    join b in _subContext.HeSubroUnits on new { a.Claim, a.UnitId } equals new { b.Claim, b.UnitId } into ps
                    from b in ps.DefaultIfEmpty()
                    select new SubroRcvdEntryTotals
                    {
                        SubroClaim = a.Claim,
                        Unit = a.Unit,
                        RequestedTotal = a.Repair + a.ActualCashValue + a.LossOfUse + a.AdminFee + a.Appraiser + a.DiminValue + a.Other + a.Agency,
                        ReceivedTotal = b.Repair ?? (Decimal)0
                          + b.ActualCashValue ?? (Decimal)0
                          + b.LossOfUse ?? (Decimal)0
                          + b.AdminFee ?? (Decimal)0
                          + b.Appraiser ?? (Decimal)0
                          + b.DiminValue ?? (Decimal)0
                          + b.Interest ?? (Decimal)0
                          + b.Other ?? (Decimal)0
                          + b.Agency ?? (Decimal)0

                        
                    }
                      ).ToList();



     
        }

Open in new window

0
Comment
Question by:jazzcatone
3 Comments
 

Author Comment

by:jazzcatone
ID: 39904289
OK so now I have the calculation for TableB working. However, like I said before I need to handle for a null. If there are no rows in tableB I'm screwed as it blows up with

 The cast to value type 'Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type."}

Any and all advice on how to handle for this would be greatly appreciated.

--Jason

        private List<SubroRcvdEntryTotals> GetSubroEntryTotals(int currentClaim)
        {
            
            return (from a in _subContext.HeUnits
                    where a.Claim == currentClaim
                    join b in _subContext.HeSubroUnits on new { a.Claim, a.UnitId } equals new { b.Claim, b.UnitId } into ps
                    from b in ps.DefaultIfEmpty()
                    select new SubroRcvdEntryTotals
                    {
                        SubroClaim = a.Claim,
                        Unit = a.Unit,
                        RequestedTotal = a.Repair + a.ActualCashValue + a.LossOfUse + a.AdminFee + a.Appraiser + a.DiminValue + a.Other + a.Agency,


                                ReceivedTotal = b.Repair.Value+ b.LossOfUse.Value + b.AdminFee.Value+ b.Appraiser.Value + b.DiminValue.Value + b.Interest.Value+ b.Other.Value+ b.Agency.Value
                    }
                      ).ToList();



        
        }

Open in new window

0
 
LVL 11

Accepted Solution

by:
Mihai Stancescu earned 500 total points
ID: 39911704
Hi,

Try verifying if b is null when you sum ReceivedTotal .

Like so:
private List<SubroRcvdEntryTotals> GetSubroEntryTotals(int currentClaim)
        {
            
            return (from a in _subContext.HeUnits
                    where a.Claim == currentClaim
                    join b in _subContext.HeSubroUnits on new { a.Claim, a.UnitId } equals new { b.Claim, b.UnitId } into ps
                    from b in ps.DefaultIfEmpty()
                    select new SubroRcvdEntryTotals
                    {
                        SubroClaim = a.Claim,
                        Unit = a.Unit,
                        RequestedTotal = a.Repair + a.ActualCashValue + a.LossOfUse + a.AdminFee + a.Appraiser + a.DiminValue + a.Other + a.Agency,
                        ReceivedTotal = b == null ? 0 : (b.Repair.Value+ b.LossOfUse.Value + b.AdminFee.Value+ b.Appraiser.Value + b.DiminValue.Value + b.Interest.Value+ b.Other.Value+ b.Agency.Value)
                    }).ToList();        
        }

Open in new window


Hope this helps,
Mishu
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
edit .asp files 5 30
TSQL convert date to string 4 34
Accessing variables in MySQL query 4 22
SQL Server Shrink hurting performance? 4 16
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question