Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,

I need to update values in a list... for which i have written a method but values is not get updated. please help me to identiffy the issue

My class structure

C#
 public class TaxBucket
    {
        public int Year { get; set; }
        public List<TaxBucketSub> taxbucketsub { get; set; }

    }
    public class TaxBucketSub
    {
        public int PortfolioID { get; set; }
        public CarryForwards carryforwards { get; set; }
        public CarryForwards buckets { get; set; }
        public CarryForwards currentyearvalue { get; set; }
    }
public class CarryForwards
    {
        public double taxDeduction { get; set; }
        public double interest { get; set; }
        public double dividend { get; set; }
        public double shortTermGains { get; set; }
        public double longTermGains { get; set; }
    }


What I have tried:

C#
public static void UpdateTaxBucket(List<TaxBucket> lsttaxbucket, int year, int portnum, string mainitem, string subitem, double value)
       {
           foreach (var bucket in lsttaxbucket.Where(b => b.Year == year))
           {
               foreach (var item in bucket.taxbucketsub.Where(p => p.PortfolioID == portnum))
               {
                   switch (mainitem)
                   {
                       case "buckets":
                           switch (subitem)
                           {
                               case "Interest":
                                   item.buckets.interest = 12;
                                   break;
                               case "Dividend":
                                   item.buckets.dividend =25;
                                   break;
                               case "Short":
                                   item.buckets.shortTermGains = 30;
                                   break;
                               case "Long":
                                   item.buckets.longTermGains =35;
                                   break;
                               case "Tax":
                                   item.buckets.taxDeduction = 50;
                                   break;
                               default:
                                   break;
                           }
                           break;
Posted
Updated 21-Apr-16 4:51am
Comments
Karthik_Mahalingam 21-Apr-16 11:09am    
make sure the object lsttaxbucket has sufficient value to loop through
Sergey Alexandrovich Kryukov 21-Apr-16 21:34pm    
You have much worse problem. You use strings representing data instead of data, such as "Long", "Tax", "Interest". Worse, you hard-code them, as well as 30, 35, 50... This is not supportable. This is just the opposite of programming. It cannot be useful. You have to redesign it all. For example, instead of those magic string, create some enum type. And so on...
—SA

1 solution

So start with the debugger and see exactly what is going on - most likely, one of the Where conditions is returning an empty enumeration.
We can't do that for you: we don't have access to your data!
 
Share this answer
 
Comments
jinesh sam 21-Apr-16 10:56am    
i am debugging.. cursor came to the inner section item.buckets.interest. even though its not updating that's my concern. i would like to know whether the syntax is correct or not.
OriginalGriff 21-Apr-16 11:02am    
And what happened when you tried? What was the value before the line was executed, and what was the value after execution?
jinesh sam 21-Apr-16 11:08am    
Before it was zero after the line executed also it remain same
OriginalGriff 21-Apr-16 11:11am    
So you need to look at the code for the property setter and getter.
What do they look like?
jinesh sam 21-Apr-16 11:11am    
it tried this as well
lsttaxbucket.Where(b => b.Year == year).SelectMany(t => t.taxbucketsub).Select(y => y.buckets.interest = 12);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900