Linq, aggregate Sum problem

  • Thread starter Thread starter Lothar Behrens
  • Start date Start date
L

Lothar Behrens

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
 
Lothar said:
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.
 
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/- Zitierten Text ausblenden -

- 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
 
Back
Top