Solved

Linq Sum Query

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

11 Experts available now in Live!

Get 1:1 Help Now