How do you set a model with repeating variables?

自作多情 提交于 2019-12-25 01:09:48

问题


Objective: Use the model to show total sales of an id and total sales difference for multiple periods.

Issue: Using MVC4 I have a model for a report that displays total sales. I know I can add a variable for each additional period, but I have many periods that I'd like to use to compare and show a difference. How can this be done avoiding needless repetition?

public class Report
{
     public long ID { get; set }

    public long TotalSales { get; set}

    public long TotalSales2 { get; set}

    public long TotalSalesDiff { get; set} 

 }

Thank you in advance for any suggestions and/or comments


回答1:


If I understood your scenario you have two option either create a dictionary that can create properties or use Dynamic object class

For dictionary

 public class DictionaryProperty
 {

      Dictionary<string, object> Yourproperties = new Dictionary<string, object>( );

      public object this[ string name ]
      {
            get
            {
                 if ( Yourproperties.ContainsKey( name ) )
                 {
                      return Yourproperties[ name ];
                 }
                 return null;
            }
            set
            {
                 Yourproperties[ name ] = value;
            }
      }

 }

}

And for the Dynamic object please refer following link http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.aspx

cheers

I don’t have enough points to add a comments that’s the why I make this is an answer.



来源:https://stackoverflow.com/questions/23545397/how-do-you-set-a-model-with-repeating-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!