Linq, aggregate Sum problem

Hi,

I figured out that I am unable to query for an aggregate function
(Sum) over a column I have properly filled.

The following NUnit test method will fail due to a NULL value even i
think this should not be true:

Provisionsabrechnung.UnitTests.Provisionsrechner.Test_Aggregate_Sum_with_Null:
System.InvalidOperationException : Das Objekt mit Nullwert muss einen
Wert haben.

        public void Test_Aggregate_Sum_with_Null()
        {
            ProvisionsabrechnungDataContext dataContext = new
ProvisionsabrechnungDataContext();

            Objektverkauf o1 = new Objektverkauf();
            Objektverkauf o2 = new Objektverkauf();
            Objektverkauf o3 = new Objektverkauf();

            List<Objektverkauf> ol =
dataContext.Objektverkauf.ToList();

            o1.Nettoumsatz = 0.0;
            o2.Nettoumsatz = 0.0;
            o3.Nettoumsatz = 0.0;

            ol.Add(o1);
            ol.Add(o2);
            ol.Add(o3);

            double BisherigerNettoUmsatz = dataContext.Objektverkauf
                .Where(a => a.Nettoumsatz != null).Sum(a => (double?)
a.Nettoumsatz).Value;

            Assert.AreEqual(0.0, BisherigerNettoUmsatz);
        }

Any ideas, what am I doing wrong?

Thanks

Lothar
0
Lothar
4/10/2010 10:35:40 AM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

2 Replies
846 Views

Similar Articles

[PageSpeed] 40

Lothar Behrens wrote:

> I figured out that I am unable to query for an aggregate function
> (Sum) over a column I have properly filled.
> 
> The following NUnit test method will fail due to a NULL value even i
> think this should not be true:
> 
> Provisionsabrechnung.UnitTests.Provisionsrechner.Test_Aggregate_Sum_with_Null:
> System.InvalidOperationException : Das Objekt mit Nullwert muss einen
> Wert haben.
> 
>         public void Test_Aggregate_Sum_with_Null()
>         {
>             ProvisionsabrechnungDataContext dataContext = new
> ProvisionsabrechnungDataContext();
> 
>             Objektverkauf o1 = new Objektverkauf();
>             Objektverkauf o2 = new Objektverkauf();
>             Objektverkauf o3 = new Objektverkauf();
> 
>             List<Objektverkauf> ol =
> dataContext.Objektverkauf.ToList();
> 
>             o1.Nettoumsatz = 0.0;
>             o2.Nettoumsatz = 0.0;
>             o3.Nettoumsatz = 0.0;
> 
>             ol.Add(o1);
>             ol.Add(o2);
>             ol.Add(o3);
> 
>             double BisherigerNettoUmsatz = dataContext.Objektverkauf
>                 .Where(a => a.Nettoumsatz != null).Sum(a => (double?)
> a.Nettoumsatz).Value;
> 
>             Assert.AreEqual(0.0, BisherigerNettoUmsatz);
>         }
> 
> Any ideas, what am I doing wrong?

What's the definition of the Objektverkauf, what is the type of the 
Nettoumsatz property?
And what exactly do you want to achieve with the "ol" List and those 
three Objektverkauf instances o1, o2 and o3? Adding them to the in 
memory "ol" List does not store them in the SQL data base.


-- 

	Martin Honnen --- MVP Data Platform Development
	http://msmvps.com/blogs/martin_honnen/
0
Martin
4/10/2010 11:02:48 AM
On 10 Apr., 13:02, Martin Honnen <mahotr...@yahoo.de> wrote:
> Lothar Behrens wrote:
> > I figured out that I am unable to query for an aggregate function
> > (Sum) over a column I have properly filled.
>
> > The following NUnit test method will fail due to a NULL value even i
> > think this should not be true:
>
> > Provisionsabrechnung.UnitTests.Provisionsrechner.Test_Aggregate_Sum_wit=
h_Nu=ADll:
> > System.InvalidOperationException : Das Objekt mit Nullwert muss einen
> > Wert haben.
>
> > =A0 =A0 =A0 =A0 public void Test_Aggregate_Sum_with_Null()
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 ProvisionsabrechnungDataContext dataContext =3D=
 new
> > ProvisionsabrechnungDataContext();
>
> > =A0 =A0 =A0 =A0 =A0 =A0 Objektverkauf o1 =3D new Objektverkauf();
> > =A0 =A0 =A0 =A0 =A0 =A0 Objektverkauf o2 =3D new Objektverkauf();
> > =A0 =A0 =A0 =A0 =A0 =A0 Objektverkauf o3 =3D new Objektverkauf();
>
> > =A0 =A0 =A0 =A0 =A0 =A0 List<Objektverkauf> ol =3D
> > dataContext.Objektverkauf.ToList();
>
> > =A0 =A0 =A0 =A0 =A0 =A0 o1.Nettoumsatz =3D 0.0;
> > =A0 =A0 =A0 =A0 =A0 =A0 o2.Nettoumsatz =3D 0.0;
> > =A0 =A0 =A0 =A0 =A0 =A0 o3.Nettoumsatz =3D 0.0;
>
> > =A0 =A0 =A0 =A0 =A0 =A0 ol.Add(o1);
> > =A0 =A0 =A0 =A0 =A0 =A0 ol.Add(o2);
> > =A0 =A0 =A0 =A0 =A0 =A0 ol.Add(o3);
>
> > =A0 =A0 =A0 =A0 =A0 =A0 double BisherigerNettoUmsatz =3D dataContext.Ob=
jektverkauf
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 .Where(a =3D> a.Nettoumsatz !=3D null).=
Sum(a =3D> (double?)
> > a.Nettoumsatz).Value;
>
> > =A0 =A0 =A0 =A0 =A0 =A0 Assert.AreEqual(0.0, BisherigerNettoUmsatz);
> > =A0 =A0 =A0 =A0 }
>
> > Any ideas, what am I doing wrong?
>
> What's the definition of the Objektverkauf, what is the type of the
> Nettoumsatz property?
> And what exactly do you want to achieve with the "ol" List and those
> three Objektverkauf instances o1, o2 and o3? Adding them to the in
> memory "ol" List does not store them in the SQL data base.
>
> --
>
> =A0 =A0 =A0 =A0 Martin Honnen --- MVP Data Platform Development
> =A0 =A0 =A0 =A0http://msmvps.com/blogs/martin_honnen/- Zitierten Text aus=
blenden -
>
> - Zitierten Text anzeigen -

The type of Nettoumsatz is at least a numeric (as I am passing 0.0). I
supposed, one would spot this :-)

It is now solved. I have not done a call to InsertOnSubmit for the
list of objects and also missed the call to SubmitChanges.

Thanks
0
Lothar
4/10/2010 4:17:47 PM
Reply:

Similar Artilces:

date criteria problem 12-19-07
I am trying to create a query that looks for date values that are greater than or equal to a date value on an open form: >=[forms]![counselorreportmenu]![txtbegin] This isn't working. The field on the form is a date format field. What can I do to get this to work? I had a similar problem in code on a form where I was trying to find dates greater than a string date value. Wondering if it is a formatting issue on the returned value...? Thanks in advance! NEVERMIND! I just used DateValue and now it's working. Thanks anyway! "rs0905" wrote: > I am trying to...

Something to dig in... Password problem
Hello! Im running Outlook 2003 on Windows XP SP1 and i can successfully connect to my Exchange 2003 server over the Internet using RPC over HTTP. The problem is that i want to store my password so i dont have to type it in everytime. Is it possible? There is no checkbox for saving it... Please help!! =) Br, Magnus G�ransson ...

MS029 (KB912442) problem... need a script
Hi all, We have Exchg. 2003 SP2 and have installed patch MS06-029. On addiction to this, we can't use Mailbox Store >- Security for adding more users on whole Store. Basiclly, we have to change OU. Question: I can export all of my key account people to .txt or csv... There are a lot of OU on whole A.D. Could I try to start script on A.D. root who will change (Add) Send As right for these people (not in the same OU). If yes, I need a script :-) Thnx a lot! Here's a good place to start. http://support.microsoft.com/default.aspx?scid=kb;en-us;237677 "Dan" <d...

Problems with Pivot Table Field Sorting in Excel 2002
I cannot get some data fields in pivot tables to appear in the "data source order". This option under advanced is greyed out for some reason. So everything gets sorted. Any suggestions? If you right-click on a field button, choose Field Settings, and click Advanced, that field should appear in the field list for sort order. Mike wrote: > I cannot get some data fields in pivot tables to appear > in the "data source order". This option under advanced is > greyed out for some reason. So everything gets sorted. > Any suggestions? -- Debra Dalgleish Excel...

listview refresh problem
This problem appears in just one computer. After fill up the listview with lines, values are not showed. But, when we click in a line this line is showed, and if we click in several lines, they are showed one by one. This computer has Windows 2000 with service packs like the others. What could be? Thanks. Rui Oliveira Is there any other mass of programs running? GDI space might be a limitation. That's all I can think of at the moment. joe On Mon, 17 Dec 2007 07:35:02 -0800, Rui Oliveira <RuiOliveira@discussions.microsoft.com> wrote: >This problem appears in just o...

Outlook 2000 upgrade problems
I'm currently on Outlook 2000 SP-3 (9.0.0.6627), (but Belarc says I've still got v 9.0.6604) having installed a proffered "improvement" from Microsoft. I can see several changes, including a) an inability now to display new address book entries in the form "Smith, John" rather than John Smith; and b) an inability to send emails from any of my mailboxes - it now insists on sending from my default mailbox on behalf of the other mailbox. Can anyone advise whether, and how, I can revert to the previous version? Thanks in anticipation ...

A problem with using DataGridView (.NET Windows Forms) in CDialog
I have a very simple CDialog-based application (VC++ 8.0/2005), where a DataGridView control is included through the usage of CWinFormsControl class. I experience the following problem with this application. If I switch to a different application (Alt+Tab, for example) while a grid cell is remaining in editing mode and then go back to my application, the application hangs (stops responding). The problem can be reproduced using CWinFormsControl and CWinFormsView classes, but it is NOT reproducible when using CFrameWnd and CWinFormsView classes or when using DataGrid control instead of t...

dual post of "setting folder option" problem
Hi, when I right click on my Deleted Items folder or my "Junk E-mail" I have the option to "empty" the folder, however, in my "Spam" folder, like all the other folders I created, I don't have that option. But I want that option. I would greatly appreciated any help with this. Thank you, Gordy Outlook does not create a "Spam" folder. It creates Junk Mail folders. You may have a third party software that does this - in that case, apply your suggestion to the makers of that software. -- Milly Staples [MVP - Outlook] Post all replies to the ...

Problems with ReportViewer and RDLC
Hello, I have an ASP.NET application that contains reports. I am not allowed to use Crystal, so I decided to use .NET's reporting tool. My problem is that the report viewer does not show in the browser, neither I am receiving any error messages. Below is the code. Any help will be really appreciated. Thank you very much for your help. Carlos Private Sub Show_Report(ByVal From_rpt_date As Date, ByVal To_rpt_date As Date) 'Set the processing mode for the ReportViewer to Local ReportViewer1.Reset() ReportViewer1.ProcessingMode = ...

Chart series display problem
At the end of my chart I always have a vertical line that is going to 0. That is caused because the cells that the chart draw contains formulas. The outcome of the formulas is space but the chart sees them as zeroes. What I would like to do is the use somthing like the formula below using the indirect function inside a series function so I can always know which is my last cell to draw that contains information: =SERIES(Investment!$G$2,Investment!$B$31:$C$181,Investment!$M$31:INDIRECT(CELL("address",A162)),1) Unfortunatelly it is not acceptible by Excel. What can I use instead?...

Problem opening Money file on another computer
I have been using Money 2006 deluxe. I make my backups on a Western Digital My Book. My computer running Vista died. I loaded Money 2006 on my XP computer and am trying to open the .mny files I had been using on Vista. Money 2006 ask me for my password and after I type it in, it says I typed in the wrong password. I am positive I typed it in correctly. Could there be an issue using a Vista Money file on XP? I have aslo tried opening a .mbf file and get the same password refusal. Any ideas. Thank you, That was a question? If so, then, yes. I suggest you make completely sure (He...

Problem with CHtmlCtrl in Windows 98
Hello, I read a post in this forum "HTML display inside MFC Dialog (Not CDHTMLDialog!)" that lead me to an implementation of a CHtmlCtrl in the August 2003 issue of MSDN Magazine's C++ Q & A. I put this into an MFC application that I am working in and it works like a charm on my Windows 2000 development machine. However, when I run it on our Windows 98 test machine, the CHtmlCtrl blows up inside the SetHTML function at the line where it tries to use IHTMLDocument2's open function to open the Html Document. The crash basically tells me that Runtime has been requeste...

Problem with Sync to Google Calendar
I’m having a minor problem when I sync my Outlook 2007 calendar with my Google calendar. I can’t tell whether the problem is in Outlook, Google or the Google Calendar Sync utility (v0.9.3.5). I don’t know if this matters but my Google calendar pre-dates my use of Outlook. I’ve been using Google Calendar for over a year but just started using Outlook in the past couple of months. (Still an Outlook newbie.) I'm running Windows 7 Ultimate 32bit. I had a recurring event on my Google calendar for 10AM-12PM every Tuesday in May, 2010. That synced to Outlook with no problem...

Nested IIF problem
Hi [PayReceived] is a tick box on a form from a subscriptions table. [DateDifference] calculates the number of dates between the [DueDate] and Today’s Date to work out if the subscription payment is overdue by 30 days or more and [MembStatus] holds a string depending on how many days overdue the payment is. I am having problems with the nested IIf statement which should calculate [MembStatus] and no matter what I do I either get a syntax error, too many brackets error or an error about commas – and I’ve been trawling through the questions and answers on this subject and tr...

import external data problem
I'm stumped. I have a file I update on a weekly basis by adding a sheet and importing external data into the newly created sheet. The source data is a .xls file. The data is contiguous and the file is roughly 100kb. The target file, holds 13 of the source files and also has a couple extra sheets for summary info. The file size with all of this is approx 2 Megs. Now I'm trying to make a similar tool for a new application. Each source file is 2.5 Megs (.xls) and the destination file is roughly 30 Megs. The data in the source file(s) is not contiguous. There is a partial row an...

Bank One/Chase download problems. $2004 Premium
I have been getting a bunch of "starting Balance adjustments when I download this credit card. Three or four so far this month. I balance my credit cards monthly, payments and charges are downloaded correctly. Why would the starting balance change several times between balancing? I could see it happening once if I made a mistake in the balancing the account but not multiple times a month. I use Money to download, not MS. Perplexed. ...

Outlook 2002
I have two users that when they click "send" the message goes into the outbox and will not send until "send and receive" is clicked. Both have the "Send Immediately When Connected" box selected. Also, one user cannot send more than one message at a time. If he has more than one message in the outbox, he has to remove all but one and do manual send and receive, then move 1 message to the outbox and repeat for all messages. Both users have laptops with XP Pro, Office XP Pro, and POP3 mail. Art ...

Scheduling contingency via non-working time in calendar -problems
Hi all I'm scheduling a game software development. Notwithstanding , or ignoring inaccurate estimations for a moment, I'm assuming 3 weeks work and 1 week contigency in each working month. Experience tells me that contingency is about right over the space of a month due to slippage, change of spec or other game dev 'issues' that happen, but this rarely works if applied right down to individual task estimations or disciplines (of which there are many). In the calendar I've set the last week of each month as nonworking to layout the basic schedule. Now I...

Problems with contact creation
Dear, I´m having problem on the external contact creation. My problem is, all times that I try to create a external contact the Windows show me a error message, the ID message is c00706ba and message is "THE RPC SERVER IS NOT AVALIABLE". After I execute the "netstat -an" command I verified that my computer try to connect a exchange server outside my server on the 445 port, but the SMB port is blocked no firewall. My question is: is there a way where I can set my computer to comunnicate with the Exchange Server in my site?!!!! Please help me On Mon, 31 Jan 20...

CRM Mobile: First time device use problem
Installed CRM Mobile with no problems on the server, verified by running CRM Mobile Customisation - works. Installed it on the device with Activesync connection to the internal network, couldn't get the device registered. My configuration is: Windows SBS 2003, CRM installed n the same SBS box, using separate IP address. The problem seems to be with SQL Server CE communicating with the SQL Server on the CRM server. I am able to browse to the ssCERDA virtual directory from everywhere, including the device. Certificates seem to be OK, although tried disabling them with no luck. Unregisterin...

Import / Export Problem
I'm having problem importing contacts from Excel to Outlook. The problem happens with phone numbers. The Outlook translator subtracts 8589934592 from the original phone number. For example. if i have 9177934249 in Excel, once imported the number becomes 587999657 when displayed in Outlook. Is this a bug? What's the fix? Also, when exporting contacts from Outlook to Excel, the output for phone numbers (business, fax, mobile, etc) is 0. Again, bug? what's the fix? Thanks ...

Export to Excel Problem
Dear all, I'm trying export a search result from the Form View by using the following command. Public Sub cmdExport_Click() Dim fileName As String fileName = InputBox("Export Excel file name:", "Enter file name") DoCmd.OutputTo acForm, "SearchRecords", "MicrosoftExcel(*.xls)", "c:\" & fileName & ".xls", True, "" End Sub However, as a result, for all the fields which the field type are MEMO and containing data exceed 255 characters were cutted down to only 255 characters in the exported excel file....

Chart problem.
I have a chart that plots body mass index, ie value on y axis, and dates on x axis. I have also added a second category x as I have divided the chart into horizonatl zones with different colors (ie underweight, overweight etc). Things work fine except that when I plot, for instance, three dates (Jan 1, Feb 1, March 1). The Jan point is right on the left side Y axis, the Feb is mid way across the chart, and the March is on the other end of the chart (right Y axis) I'd like to narrow down the spaces between each data point, and also have some space at the beginning of the chart ...

RULES Problem on OL2K3
hey All; I have a few rules et up in OL2K3 and they were working fine until a week ago. One rule still works, but the rest will not work until I go into it and tell it to Run Rules Now and select each one. very annoying. Is there a fix for this or does anyone know why this happened and what I can do? The Rules still work when I run them automatically. I receive emails from Exchange 2003, but it can't be that because they were working before. TIA ...

Inventory Transaction Problem
Hi, I am using the Microsoft Inventory Template and the Order Management Templates which I have combined into one database, therefore the Add an Order and details Form now looks in the products form from the inventory side etc. Everything is working except when I put a new order through I need the Inventory Transactions to add the transaction as a sale, hence the Units on hand would decrease by qty sold. I have created a query to show the order details i.e Order Date, Product Id and Qty Sold, and here is the problem - how do I get the Inventory transactions table to update fro...