Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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