John.P
John.P

Reputation: 657

Calculate the sum of property

I'm trying to calculate the sum of the property name called Total to GrandTotal. What I want it to do is that GrandTotal calculates the sum of all Total, this is how I tried it:

public class ItemProperties
        {
            public int Item { get; set; }
            public string Description { get; set; }
            public int Quantity { get; set; }
            public int UnitPrice { get; set; }
            public int Tax { get; set; }
            public int TotalTax { get { return ((Quantity * UnitPrice) * Tax) /100 ; } }
            public int Total { get { return (Quantity * UnitPrice) + TotalTax; } }

            public int GrandTotal
            {
                get
                {
                    foreach (var l in Total) //Error
                        l += Total;          // Error
                    return l;                //Error
                }
            }
        } 

The class ItemProperties is added with the Add() method two times likes this: (So Total is having a different values with each add)

 Items.Add(new ItemProperties {
    Item = Convert.ToInt32(lines[i]),
    Description = lines[i + 1],
    Quantity = Convert.ToInt32(lines[i + 2]),
    UnitPrice = Convert.ToInt32(lines[i + 3]),
    Tax = Convert.ToInt32(lines[i + 4])
});
Items.Add(new ItemProperties {
    Item = Convert.ToInt32(lines[i + 5]),
    Description = lines[i + 6],
    Quantity = Convert.ToInt32(lines[i + 7]),
    UnitPrice = Convert.ToInt32(lines[i + 8]),
    Tax = Convert.ToInt32(lines[i + 9])
});

Don't worry about the lines variables, they're not relevant.. The only thing I want is that the GrandTotal calculate the sum of all Total properties.

Upvotes: 1

Views: 1904

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

You cannot calculate GrandTotal on an individual item. You need to do it on the collection of items. The property does not make sense on an individual item, and should be removed.

C# provides an easy way of computing the total:

var grandTotal = Items.Sum(item => item.Total);

If you need to explse a class that has GrandTotal property, wrap a list in a custom class, and put the property on the ItemPropertiesList class.

Upvotes: 3

slugster
slugster

Reputation: 49974

You cannot execute a foreach on something that is not enumerable - and your Total property is an int, not an IEnumerable.

Your GrandTotal property should probably be in the class that contains the collection of ItemProperties, your code would look something similar to this:

public ItemProperties[] MyItems { get; set; }

public int GrandTotal 
{
    get 
    {
        var total = 0;
        foreach (var item in MyItems)
            total += item.Total;

        return total;
    }
}

Or if you wanted to use LINQ:

public List<ItemProperties> MyItems { get; set; }

public int GrandTotal 
{
    get {  return MyItems.Sum(item => item.Total); }
}

Note that I've kept this simple and not included any null checking code etc - that's an exercise for you to complete.

Upvotes: 5

Dan Field
Dan Field

Reputation: 21641

Total is not an IEnumerable (such as a List). It doesn't make sense to try to provide a GrandTotal property on a single Item right? What you probably want here is a way to get a GrandTotal for a List<ItemProperties>, which you would use code very similar to what you have:

List<ItemProperties> ListOfItems = new List<ItemProperties>();
// fill the list
int sum;

foreach (ItemProperties ip in ListOfItems)
{
    sum += ip.Total;
}

You could also use LINQ to do this in place of an explicit foreach loop:

int sum = ListOfItems.Sum(x => x.Total);

Upvotes: 1

Related Questions