c#对象属性在执行计算时保持相邻关系

c#对象属性在执行计算时保持相邻关系,c#,dictionary,nearest-neighbor,C#,Dictionary,Nearest Neighbor,我有一个包含months属性的类: public class Item { public Item() { } public decimal Jan { get; set; } public decimal Feb { get; set; } public decimal Mar { get; set; } public decimal Apr { get; set; } public decimal May { get; set; }

我有一个包含months属性的类:

public class Item
{
   public Item()
   {

   }

   public decimal Jan { get; set; }
   public decimal Feb { get; set; }
   public decimal Mar { get; set; }
   public decimal Apr { get; set; }
   public decimal May { get; set; }
   public decimal Jun { get; set; }
   public decimal Jul { get; set; }
   public decimal Aug { get; set; }
   public decimal Sep { get; set; }
   public decimal Oct { get; set; }
   public decimal Nov { get; set; }
   public decimal Dec { get; set; }
}
还有一本
字典

到目前为止还不错。一切都很好

问题是:我需要忽略
Item
类中的某些months属性。被忽略的月份的值为0

如果字典包含以下值:

Dictionary<string, decimal> values = new Dictionary<string, decimal>();
values.add("Feb", 10.00);
values.add("Mar", 10.00);
values.add("Apr", 10.00);
values.add("Jun", 10.00);
values.add("Jul", 10.00);
values.add("Aug", 10.00);
values.add("Oct", 10.00);
values.add("Nov", 10.00);
values.add("Dec", 10.00);
编辑:

更清楚地说:

当字典中提供了所有月份的数据时,我的计算工作正常

  • 项目.Jan=价值[“Jan”]
  • 项目2月=项目1月+价值[“2月”]
  • 诸如此类
当字典中缺少某些月份时,计算应如下所示:

  • 缺少的月份的值为0
  • dictionary中的第一个键将定义Item类中的第一个属性,该属性的值=dictionary[“key”]值

我唯一的问题是维护与前一项的关系,以防May丢失,item.Jun=item.Apr+value[“Jun”]等等。

您可以使用TryGetValue从字典中提取值,而不知道键是否存在

decimal temp;
decimal runningTotal = 0m;

// Jan doesn't exist, so temp will default to zero
values.TryGetValue("Jan", out temp);
runningTotal += temp;
item.Jan = (temp == 0m ? temp : runningTotal);

values.TryGetValue("Feb", out temp);
runningTotal += temp;
item.Feb = (temp == 0m ? temp : runningTotal);

values.TryGetValue("Mar", out temp);
runningTotal += temp;
item.Mar = (temp == 0m ? temp : runningTotal);

values.TryGetValue("Apr", out temp);
runningTotal += temp;
item.Apr = (temp == 0m ? temp : runningTotal);

// May doesn't exist, so temp will default to zero
// and Item.May get a zero for its value 
// runningTotal is still set to the value of Apr
values.TryGetValue("May", out temp);
runningTotal += temp;
item.May = (temp == 0m ? temp : runningTotal);

// Resume the sums with the value of Jun
values.TryGetValue("Jun", out temp);
runningTotal += temp;
item.Jun = (temp == 0m ? temp : runningTotal);

... and so on for the other months

同样的答案,也使用TryGetValue,a提出了以下两种方法:

    public decimal GetValuesFromDictionary(Dictionary<string, decimal> values)
    {
        Jan = GetValueFromKey(values, "Jan");
        Feb = Jan + GetValueFromKey(values, "Feb");  
        Mar = Feb + GetValueFromKey(values, "Mar");
        Apr = Mar + GetValueFromKey(values, "Apr");
        May = Apr + GetValueFromKey(values, "May");
        Jun = Apr + GetValueFromKey(values, "Jun"); 
        Jul = Jun + GetValueFromKey(values, "Jul");
        Aug = Jul + GetValueFromKey(values, "Aug");
        Sep = Aug + GetValueFromKey(values, "Sep");
        Oct = Sep + GetValueFromKey(values, "Oct"); 
        Nov = Oct + GetValueFromKey(values, "Nov");
        Dec = Nov + GetValueFromKey(values, "Dec");

        return Dec;
    }

    private decimal GetValueFromKey(Dictionary<string,decimal> values, string key)
    {
        decimal temp;

        values.TryGetValue(key, out temp);

        return temp;
    }
public decimal getvalues fromdocumentary(字典值)
{
Jan=GetValueFromKey(值,“Jan”);
Feb=Jan+GetValueFromKey(值,“Feb”);
Mar=Feb+GetValueFromKey(值,“Mar”);
Apr=Mar+GetValueFromKey(值,“Apr”);
May=Apr+GetValueFromKey(值,“May”);
Jun=Apr+GetValueFromKey(值,“Jun”);
Jul=Jun+GetValueFromKey(值,“Jul”);
Aug=Jul+GetValueFromKey(值,“Aug”);
Sep=Aug+GetValueFromKey(值,“Sep”);
Oct=Sep+GetValueFromKey(值,“Oct”);
Nov=Oct+GetValueFromKey(值,“Nov”);
Dec=Nov+GetValueFromKey(值,“Dec”);
返回12月;
}
私有十进制GetValueFromKey(字典值、字符串键)
{
十进制温度;
TryGetValue(键,输出温度);
返回温度;
}
使用值测试您的案例是否正确

Dictionary<string, decimal> values = new Dictionary<string, decimal>();
            values.Add("Feb", 10);
            values.Add("Mar", 10);
            values.Add("Apr", 10);
            values.Add("Jun", 10);
            values.Add("Jul", 10);
            values.Add("Aug", 10);
            values.Add("Sep", 10);
            values.Add("Oct", 10);
            values.Add("Nov", 10);
            values.Add("Dec", 10);

            Decimal total = 0;
            Item Item = new Item();

        total = Item.GetValuesFromDictionary(values);
字典值=新字典();
添加(“2月”,10日);
增加(“3月”,第10页);
增加(“4月”,10日);
增加(“6月”,第10页);
增加(“7月”,10日);
添加(“8月”,10日);
添加(“9月”,第10页);
添加(“10月”,10日);
增加(“11月”,10日);
增加(“12月”,10日);
小数总数=0;
项目=新项目();
总计=项。从字典中获取值(值);

返回给我正确的值=D

保留一个名为
PreviousValid
的十进制类型值,并最初将其设置为
Items[“Jan”]。现在从更改代码

item.Jun = item.Apr + value["Jun"];
喜欢

PreviousValid = (item.Apr != null) ? item.Apr : PreviousValid;
item.Jun = PreviousValid + value["Jun"];

对所有“item.xxx”分配进行此修改,您的代码应该可以正常工作。

似乎是一个尝试:item.Jan=values.ContainsKey(“Jan”)?值[“Jan”]:0;正是我要找的!现在你被标记在我的书中的“我的英雄”页面上!
Dictionary<string, decimal> values = new Dictionary<string, decimal>();
            values.Add("Feb", 10);
            values.Add("Mar", 10);
            values.Add("Apr", 10);
            values.Add("Jun", 10);
            values.Add("Jul", 10);
            values.Add("Aug", 10);
            values.Add("Sep", 10);
            values.Add("Oct", 10);
            values.Add("Nov", 10);
            values.Add("Dec", 10);

            Decimal total = 0;
            Item Item = new Item();

        total = Item.GetValuesFromDictionary(values);
item.Jun = item.Apr + value["Jun"];
PreviousValid = (item.Apr != null) ? item.Apr : PreviousValid;
item.Jun = PreviousValid + value["Jun"];