Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linq Sum Query

Posted on 2014-03-03
3
Medium Priority
?
609 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

609 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