问题
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