Solved

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

Posted on 2012-03-28
3
315 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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