简体   繁体   English

从列表对象双精度类型中查找最大单元格索引和值

[英]Finding Max cell index and value from list object double type

I have the following code which generates 4 lists in an object (each contains 24 cells): 我有以下代码在一个对象中生成4个列表(每个列表包含24个单元格):

    public class MyDataObject
    {
        public double AmountNeed { get; set; }
        public double TotalLose { get; set; }
        public double TotalGain { get; set; }
        public double TotalCost { get; set; }
    }

var L = new List<MyDataObject>();
for (int z=0; z < list_Exp.Count; z++)
    {

     var d = new MyDataObject();

     d.AmountNeed =Math.Ceiling((goalexp - currentexp) / (list_Exp[z]));
     d.TotalLose = d.AmountNeed * (list_Amount_MadeFrom_One[z] * list_BuyPrice_MadeFrom_One[z] + list_Amount_MadeFrom_Two[z] * list_BuyPrice_MadeFrom_Two[z]);
     d.TotalGain = d.AmountNeed * list_AmountMade[z] * list_SellPrice[z];
     d.TotalCost = d.TotalGain - d.TotalLose;
     L.Add(d);

   }

dataGrid.ItemsSource = L;

Each one of the lists is double type. 列表中的每一个都是double类型。

Once i have finished creating the lists, i want to find the max and min value of d.TotalCost. 创建完列表后,我想找到d.TotalCost的最大值和最小值。

The final goal is to find the min and max row index so i can color those lines in the datagrid. 最终目标是找到minmax行索引,以便我可以在数据网格中为这些行着色。

Thank You. 谢谢。

This can be accomplished by LINQ. 这可以通过LINQ完成。

First make your MyDataObject implement IComparable<MyDataObject> . 首先,使您的MyDataObject实现IComparable<MyDataObject> Implement the CompareTo method like this: 像这样实现CompareTo方法:

public int CompareTo(MyDataObject other) {
    return this.TotalCost.CompareTo(other.TotalCost);
}

Then, you can just use this find out the index of the data object with the largest total cost: 然后,您可以使用此方法找出总成本最大的数据对象的索引:

L.IndexOf(L.Max())

An alternative way is to do this: 另一种方法是执行此操作:

L.FindIndex(x => x.TotalCost == L.Max(y => y.TotalCost))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM