C# 中List相关使用方法整理

1、取出classList中不包含rightClassList中成员,生成新的missingClassList

        private List<FireInfo> classList= new List<FireInfo>();
        private List<FireInfo> rightClassList = new List<FireInfo>();
        private List<FireInfo> missingClassList = new List<FireInfo>();   

        
    //取出缺少的--
   missingClassList = classList.Where(x => !rightClassList.Any(a => x.name == a.name)).ToList();

2、修改List中指定元素的值

  List<PointData> points = rightPoints.Where(x => x.pointTrs.name.Equals(args.transform.name)).ToList();
  points.ForEach((p) =>
  {
      p.pointTime = (int)phaseTime;
      p.pointTrs = args.transform;
      CommonFunc.SetTrs(p.selectIcon, true);
      if (p.selectIcon2)
          CommonFunc.SetTrs(p.selectIcon2, true);
      //截图
      OperationPhoto.Instance.GetOperationPhoto((Texture2D texture2D, string photoStr) =>
      {
          p.photo = UIManager_01.Instance.ShowPhotos(texture2D);
      });
  });

3、List 中Select、Any、Where、GroupBy的应用

    public void CarRunStatisticsInfoRefresh(CarRunStatisticsInfo[] carRunStatisticsInfos)
    {
        // foreach (var carRunStatisticsInfo in carRunStatisticsInfos)
        {

            IEnumerable<Vector2> thisMonthList = carRunStatisticsInfos.Select(e => new Vector2(e.timeVariable, (float)e.taskTimeCountThis));
            IEnumerable<Vector2> lastMonthList = carRunStatisticsInfos.Select(e => new Vector2(e.timeVariable, (float)e.taskTimeCountLast));
            List<Vector2> vector2ThisMonthList = new List<Vector2>();
            List<Vector2> vector2LastMonthList = new List<Vector2>();
            carRunStatisticsSeries_ThisMonth.theGraph.xAxis.AxisMinValue = 1;
            carRunStatisticsSeries_ThisMonth.theGraph.xAxis.AxisMaxValue = dateList.daysInMonth;
            carRunStatisticsSeries_ThisMonth.theGraph.xAxis.AxisNumTicks = (int)dateList.daysInMonth + 1;

            if (carRunStatisticsInfos.Select(e => e.taskTimeCountThis).Max() >= carRunStatisticsInfos.Select(e => e.taskTimeCountLast).Max())
            {
                if (carRunStatisticsInfos.Select(e => e.taskTimeCountThis).Max() > 0)
                {
                    carRunStatisticsSeries_ThisMonth.theGraph.yAxis.AxisMaxValue = (float)carRunStatisticsInfos.Select(e => e.taskTimeCountThis).Max();
                }

                // Debug.LogError("本月" + carRunStatisticsInfos.Select(e => e.taskTimeCountThis).Max() * 1.2f);
            }
            else
            {
                carRunStatisticsSeries_LastMonth.theGraph.yAxis.AxisMaxValue = (float)carRunStatisticsInfos.Select(e => e.taskTimeCountLast).Max();
                // Debug.LogError("上月" + carRunStatisticsInfos.Select(e => e.taskTimeCountThis).Max() * 1.2f);
            }

            for (int i = 1; i <= (int)dateList.daysInMonth; i++)
            {
                if (thisMonthList.Any(e => e.x == i))
                {
                    var v = thisMonthList.Where(e => e.x == i).First();
                    vector2ThisMonthList.Add(v);
                }
                else
                {
                    vector2ThisMonthList.Add(new Vector2(i, 0));
                }
                if (lastMonthList.Any(e => e.x == i))
                {
                    var v = lastMonthList.Where(e => e.x == i).First();
                    vector2LastMonthList.Add(v);
                }
                else
                {
                    vector2LastMonthList.Add(new Vector2(i, 0));
                }
            }

            carRunStatisticsSeries_ThisMonth.pointValues.SetList(vector2ThisMonthList);
            carRunStatisticsSeries_LastMonth.pointValues.SetList(vector2LastMonthList);
            IEnumerable<Vector2> thisYearList = carRunStatisticsInfos.Select(e => new Vector2(e.timeVariable, (float)e.taskTimeCountThis));
            IEnumerable<Vector2> lastYearList = carRunStatisticsInfos.Select(e => new Vector2(e.timeVariable, (float)e.taskTimeCountLast));
            List<Vector2> vectorsThisYearList = new List<Vector2>();
            List<Vector2> vectorsLastYearList = new List<Vector2>();
            if (carRunStatisticsInfos.Select(e => e.taskTimeCountThis).Max() >= carRunStatisticsInfos.Select(e => e.taskTimeCountLast).Max())
            {
                if (carRunStatisticsInfos.Select(e => e.taskTimeCountThis).Max() > 0)
                {
                    carRunStatisticsSeries_ThisYear.theGraph.yAxis.AxisMaxValue =
                        (float)carRunStatisticsInfos.Select(e => e.taskTimeCountThis).Max();
                }
            }
            else
            {
                carRunStatisticsSeries_LastYear.theGraph.yAxis.AxisMaxValue = (float)carRunStatisticsInfos.Select(e => e.taskTimeCountLast).Max();
            }

            for (int i = 1; i <= 12; i++)
            {
                if (thisYearList.Any(e => e.x == i))
                {
                    var v = thisYearList.Where(e => e.x == i).First();
                    vectorsThisYearList.Add(v);
                }
                else
                {
                    vectorsThisYearList.Add(new Vector2(i, 0));

                }
                if (lastYearList.Any(e => e.x == i))
                {
                    var v = lastYearList.Where(e => e.x == i).First();
                    vectorsLastYearList.Add(v);

                }
                else
                {
                    vectorsLastYearList.Add(new Vector2(i, 0));

                }
            }

            carRunStatisticsSeries_ThisYear.pointValues.SetList(vectorsThisYearList);
            carRunStatisticsSeries_LastYear.pointValues.SetList(vectorsLastYearList);
        }
    }

   

 public void ProductionLineInfoRefresh(ProductionLineInfo[] productionLineInfos)
    {
        if (productionLineInfos.Length > 0)
        {
            IEnumerable<IGrouping<string, ProductionLineInfo>> lineInfos =
                productionLineInfos.GroupBy(e => e.productionLineName);
            foreach (IGrouping<string, ProductionLineInfo> productionLine in lineInfos)
            {
                if (productionLine.Key == "生产线一")
                {
                    line1TotalProductiveCapacity = 0;
                    foreach (var info in productionLine)
                    {

                        line1TotalProductiveCapacity += (float)info.productionCapacity;
                    }
                    Debug.LogError("生产线一生产能力总和:" + line1TotalProductiveCapacity);

                    productivityLine1Series.theGraph.xAxis.AxisMinValue = 1;
                    productivityLine1Series.theGraph.xAxis.AxisMaxValue = dateList.daysInMonth;
                    productivityLine1Series.theGraph.xAxis.AxisNumTicks = (int)dateList.daysInMonth + 1;

                    if (productionLine.Select(e => e.productionCapacity).Max() > 0)
                    {
                        //if (productionLine.Select(e => e.productionCapacity).Max().ToString().Length > 5)
                        //{
                        //    productivityLine1Series.yAxis.AxisTitleString = "单位:吨";
                        //    productivityLine1Series.theGraph.yAxis.AxisMaxValue =
                        //        (float)productionLine.Select(e => e.productionCapacity).Max() / 1000 * 1.5f;

                        //    //将获取生产线一某月每日的数据存储
                        //    IEnumerable<Vector2> lineOneThisMonth = productionLine.Select(e =>
                        //        new Vector2(e.timeVariable, (float)e.productionCapacity / 1000f));
                        //    //定义存储生产线一某月每日数据
                        //    List<Vector2> lineOneThisMonthVector2 = new List<Vector2>();
                        //    //添加某月中每天的数据并存储
                        //    for (int i = 1; i <= (int)dateList.daysInMonth; i++)
                        //    {
                        //        if (lineOneThisMonth.Any(e => e.x == i))
                        //        {
                        //            var v = lineOneThisMonth.Where(e => e.x == i).First();
                        //            lineOneThisMonthVector2.Add(v);
                        //        }
                        //        else
                        //        {
                        //            lineOneThisMonthVector2.Add(new Vector2(i, 0));
                        //        }
                        //    }

                        //    //添加生产线一柱状图数据
                        //    productivityLine1Series.pointValues.SetList(lineOneThisMonthVector2);
                        //}
                        //else
                        {
                            productivityLine1Series.yAxis.AxisTitleString = "单位:千克";
                            productivityLine1Series.theGraph.yAxis.AxisMaxValue =
                                (float)productionLine.Select(e => e.productionCapacity).Max();
                            IEnumerable<Vector2> lineOneThisMonth = productionLine.Select(e =>
                                    new Vector2(e.timeVariable, (float)e.productionCapacity));
                            List<Vector2> lineOneThisMonthVector2 = new List<Vector2>();

                            for (int i = 1; i <= (int)dateList.daysInMonth; i++)
                            {
                                if (lineOneThisMonth.Any(e => e.x == i))
                                {
                                    var v = lineOneThisMonth.Where(e => e.x == i).First();
                                    lineOneThisMonthVector2.Add(v);
                                }
                                else
                                {
                                    lineOneThisMonthVector2.Add(new Vector2(i, 0));
                                }
                            }

                            productivityLine1Series.pointValues.SetList(lineOneThisMonthVector2);
                        }
                    }
                }
                else if (productionLine.Key == "生产线二")
                {

                    line2TotalProductiveCapacity = 0;
                    foreach (var info in productionLine)
                    {
                        line2TotalProductiveCapacity += (float)info.productionCapacity;

                    }
                    Debug.LogError("生产线二生产能力总和:" + line2TotalProductiveCapacity);

                    productivityLine2Series.theGraph.xAxis.AxisMinValue = 1;
                    productivityLine2Series.theGraph.xAxis.AxisMaxValue = dateList.daysInMonth;
                    productivityLine2Series.theGraph.xAxis.AxisNumTicks = (int)dateList.daysInMonth + 1;

                    if (productionLine.Select(e => e.productionCapacity).Max() > 0)
                    {
                        //if (productionLine.Select(e => e.productionCapacity).Max().ToString().Length > 5)
                        //{
                        //    productivityLine2Series.theGraph.yAxis.AxisMinValue =
                        //        (float)productionLine.Select(e => e.productionCapacity).Max() / 1000 * 1.5f;
                        //    //将获取生产线二今年每月的数据存储
                        //    IEnumerable<Vector2> line2ThisYear = productionLine.Select(e =>
                        //        new Vector2(e.timeVariable, (float)e.productionCapacity / 1000f));
                        //    //定义存储生产线二今年每月数据
                        //    List<Vector2> line2ThisYearVector2 = new List<Vector2>();
                        //    //添加一年中每月的数据
                        //    for (int i = 1; i <= (int)dateList.daysInMonth; i++)
                        //    {
                        //        if (line2ThisYear.Any(e => e.x == i))
                        //        {
                        //            var v = line2ThisYear.Where(e => e.x == i).First();
                        //            line2ThisYearVector2.Add(v);
                        //        }
                        //        else
                        //        {
                        //            line2ThisYearVector2.Add(new Vector2(i, 0));
                        //        }
                        //    }

                        //    //添加生产线二柱状图数据
                        //    productivityLine2Series.pointValues.SetList(line2ThisYearVector2);
                        //}
                        //else
                        {
                            productivityLine2Series.theGraph.yAxis.AxisMinValue =
                                (float)productionLine.Select(e => e.productionCapacity).Max();
                            IEnumerable<Vector2> line2ThisYear = productionLine.Select(e =>
                                  new Vector2(e.timeVariable, (float)e.productionCapacity));
                            List<Vector2> line2ThisYearVector2 = new List<Vector2>();
                            for (int i = 1; i <= (int)dateList.daysInMonth; i++)
                            {
                                if (line2ThisYear.Any(e => e.x == i))
                                {
                                    var v = line2ThisYear.Where(e => e.x == i).First();
                                    line2ThisYearVector2.Add(v);
                                }
                                else
                                {
                                    line2ThisYearVector2.Add(new Vector2(i, 0));
                                }
                            }

                            productivityLine2Series.pointValues.SetList(line2ThisYearVector2);
                        }
                    }

                }

            }

            Debug.LogError("生产能力总和:" + line1TotalProductiveCapacity);
            //if (line1TotalProductiveCapacity.ToString().Length > 8)
            //{
            //    totalProductionLine1Unit.text = "吨";
            //    totalProductionLine1.text = (line1TotalProductiveCapacity / 1000f).ToString("0.00");
            //}
            //else
            {
                totalProductionLine1Unit.text = "千克";
                totalProductionLine1.text = line1TotalProductiveCapacity.ToString("0.00");
            }

            //if (line2TotalProductiveCapacity.ToString().Length > 8)
            //{
            //    totalProductionLine2Unit.text = "吨";
            //    totalProductionLine2.text = (line2TotalProductiveCapacity / 1000f).ToString("0.00");
            //}
            //else
            {
                totalProductionLine2Unit.text = "千克";
                totalProductionLine2.text = line2TotalProductiveCapacity.ToString("0.00");
            }

            //if ((line1TotalProductiveCapacity / dateList.daysInMonth).ToString().Length > 6)
            //{
            //    averageProductivityLine1Unit.text = "吨/天";
            //    averageProductivityLine1.text = ((line1TotalProductiveCapacity / dateList.daysInMonth) / 1000f).ToString("0.00");

            //}
            //else
            {
                averageProductivityLine1Unit.text = "千克/天";
                averageProductivityLine1.text = (line1TotalProductiveCapacity / dateList.daysInMonth).ToString("0.00");
            }

            //if ((line2TotalProductiveCapacity / dateList.daysInMonth).ToString().Length > 6)
            //{
            //    averageProductivityLine2Unit.text = "吨/天";
            //    averageProductivityLine2.text = ((line2TotalProductiveCapacity / dateList.daysInMonth) / 1000).ToString("0.00");

            //}
            //else
            {
                averageProductivityLine2Unit.text = "千克/天";
                averageProductivityLine2.text = (line2TotalProductiveCapacity / dateList.daysInMonth).ToString("0.00");
            }
            ErrorToolTip.Instance.DestroyTooltip();
        }
        else
        {

            List<Vector2> lineOneThisMonthVector2 = new List<Vector2>();

            for (int i = 1; i <= (int)dateList.daysInMonth; i++)
            {
                lineOneThisMonthVector2.Add(new Vector2(i, 0));
            }

            productivityLine1Series.pointValues.SetList(lineOneThisMonthVector2);
            productivityLine2Series.pointValues.SetList(lineOneThisMonthVector2);
            totalProductionLine1.text = "0";
            totalProductionLine2.text = "0";
            averageProductivityLine1.text = "0";
            averageProductivityLine2.text = "0";

            ErrorToolTip.Instance.DestroyTooltip();
        }


    }

4.List 中Select、Exists、Where的应用

  var tempList = rightPoints.Select(r => r.photo).ToList();
  bool isExist = tempList.Exists(t => t == parent);
  Debug.LogFormat("<color=yellow>{0}是否存在:{1}</color>", parent, isExist);
  if (isExist)
  {
      rightCount--;
      List<PointData> pointDatas = rightPoints.Where(x => x.photo == parent).ToList();
      pointDatas.ForEach(p =>
      {
          p.pointTime = 0;
          CommonFunc.SetCollider(p.pointTrs, true);
          CommonFunc.SetTrs(p.selectIcon, false);
          GameObject.Destroy(p.photo);
          p.photo = null;
      });
  }
  else {
      errorCount--;
      errorPoints.Where((val) => val.photo.name == parent.name).ToList().ForEach(e =>
      {
          CommonFunc.SetCollider(e.pointTrs, true);
          CommonFunc.SetTrs(e.selectIcon, false);
          GameObject.Destroy(e.photo);
      });
      //移除指定元素
      errorPoints = errorPoints.Where((val) => val.photo.name != parent.name).ToList();
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值