Solved

get a sum from a sql table using linq to sql visual studio 2008 c#

Posted on 2012-03-28
3
313 Views
Last Modified: 2012-03-29
I am trying to sum a field in a table.  If I use this method:

                        decimal? TotHrs2 = (from i in dc2.esna_JobAnalysis
                                   where i.emp_no == Emp.EmployeeId &&
                                   i.work_date == dt
                                   select i.hours).Sum();

I get this error:

"The null value cannot be assigned to a member with type System.Decimal which is a non-nullable value type."

This, of course works:

                        var GetHours = from i in dc.esna_JobAnalysis
                                                        where i.emp_no == Emp.EmployeeId &&
                                                        i.work_date == dt
                                                        select new { i.hours };

                        decimal TotHrs1 = 0;
                        foreach (var GH in GetHours)
                        {
                            TotHrs1 += GH.hours;
                        }

However, I'm hoping to avoid the loop.  Can't this be done with a .Sum using linq?  What am I doing wrong in the first example?

Thank you!
0
Comment
Question by:g_johnson
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37782084
I'm assuming this is LINQ2SQL.  In that case, you would get that error when i.emp_no == Emp.EmployeeId && i.work_date == dt returns no records.  Please try the following to work around this issue:
decimal? TotHrs2 = (from i in dc2.esna_JobAnalysis
		   where i.emp_no == Emp.EmployeeId &&
		   i.work_date == dt
		   select (decimal?)i.hours).Sum();

Open in new window


I hope this helps.
0
 
LVL 4

Author Closing Comment

by:g_johnson
ID: 37782132
Thank you, that worked.  If I understand what you did, we are telling linq that if there are no records, return the resulting null as a nullable decimal so that it matches our declaration.  Is that about right?
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37782240
Yes, that's correct.  I'm glad I was able to help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

20 Experts available now in Live!

Get 1:1 Help Now