如何根据同一对象列表的其他属性值分配属性值

时间:2016-11-08 13:56:17

标签: c#

我的结构

public struct lumCummulativeData
{
    public byte[] lumId { get; set; }
    public float ledPwr { get; set; }
    public float batChargePwr { get; set; }
    public float batDischargePwr { get; set; }
    public float ledPwrAvg { get; set; }
    public float batChargePwrAvg { get; set; }
    public float batDischargePwrAvg { get; set; }
    public float ledPwrWh { get; set; }
    public float batChargeWh { get; set; }
    public float batDischargeWh { get; set; }
    public int count { get; set; }
    public string lumName { get; set; } 
}

声明列表

 public static List<lumCummulativeData> cummulativeDataList
     = new List<lumCummulativeData>();

我会将值分配给lumId   cummulativeData.lumId = lumIdByte; (分配了一些id值)

现在我想编写一些条件,使得必须根据与网络数据包获取的lumId匹配的lumId来分配列表对象(同一对象)的其他属性值。

我试过的逻辑

for(int i=0; i<cummulativeDataList.Count(); i++)
{
    bool lumIdMatched = cummulativeDataList[i]
                        .lumIdCummulativeData
                        .SequenceEqual(packet.SelfID); 

    if(lumIdMatched == true)
    {
        cummulativeData.ledPwr += packet.LedPwr;
        cummulativeData.batChargePwr += packet.batchrgpwr;
        cummulativeData.batDischargePwr += packet.battdispwr;
        cummulativeData.count++;
        cummulativeDataList.Add(cummulativeData);
    }
}

列表对象正在增加,而不是将字段值添加到现有对象。这是我在分配LumId时创建的。我想要一些逻辑迭代到所有现有对象的LumId并在lumId与我从网络获得的数据包lumid匹配时添加其他字段值

2 个答案:

答案 0 :(得分:0)

我可以看到两个问题。首先在你的行上

bool lumIdMatched = cummulativeDataList[i].lumIdCummulativeData.SequenceEqual(packet.SelfID);

lumIdCummulativeData不是lumCummulativeData结构的属性,因此应该是:

bool lumIdMatched = cummulativeDataList[i].lumId.SequenceEqual(packet.SelfID);

其次,无论何时匹配,都要向cummulativeDataList添加一个新的列表元素。相反,您应该修改当前正在迭代的列表元素。

for(int i=0;i<cummulativeDataList.Count();i++)
        {
           bool lumIdMatched = cummulativeDataList[i].lumId.SequenceEqual(packet.SelfID); 
            if(lumIdMatched==true)
            {
                cummulativeDataList[i].ledPwr+= packet.LedPwr;
                cummulativeDataList[i].batChargePwr += packet.batchrgpwr;
                cummulativeDataList[i].batDischargePwr += packet.battdispwr;
                cummulativeDataList[i].count++;
            }
        }

您可以使用LINQ以不同的方式解决问题,这有点更优雅:

cummulativeDataList
  .Where(lumCD => lumCD.lumId.SequenceEqual(packet.SelfID))
  .ForEach(lumCD => {
     lumCD.ledPwr += packet.LedPwr;
     lumCD.batChargePwr += packet.batchrgpwr;
     lumCD.batDischargePwr += packet.battdispwr;
     lumCD.count++;
     }
  );

答案 1 :(得分:0)

你应该使用cummulativeDataList [i]而不是cummulativeData。

另外,检查是否真的需要添加功能。因为你想改变它而不是添加新的。

for (int i=0; i<cummulativeDataList.Count(); i++)
{
    bool lumIdMatched = cummulativeDataList[i]
                        .lumIdCummulativeData
                        .SequenceEqual(packet.SelfID); 

    if (lumIdMatched == true)
    {
        cummulativeDataList[i].ledPwr += packet.LedPwr;
        cummulativeDataList[i].batChargePwr += packet.batchrgpwr;
        cummulativeDataList[i].batDischargePwr += packet.battdispwr;
        cummulativeDataList[i].count++;
        //cummulativeDataList.Add(cummulativeData);
    }
}