Solved

Linq Sum Query

Posted on 2014-03-03
3
564 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now