【问题标题】:Create Ranges in the list在列表中创建范围
【发布时间】:2020-03-04 15:43:13
【问题描述】:

我有一个价目表

10
21
30
90
100
150
400

我想以低于 100 的 10 个增量添加价格。高于 100 以 100 为增量显示

所以我的最终价目表会是这样的

10
20
30
40
50
60
70
80
90
100
200
300
400

有没有人做过类似的事情。任何帮助或建议将不胜感激。

提前致谢

【问题讨论】:

  • 您打算如何处理原始列表中的21150?把它们扔掉?
  • 这个不清楚,原来的值呢?

标签: c# list range price enumerable


【解决方案1】:

我相信以下程序可以如您所愿:

using System;
using System.Linq;

namespace csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var prices = new int[]{10, 21, 30, 90, 100, 150, 400}.ToList();
            var finalPriceList =
                Enumerable
                .Range(0, 10)
                .Select(i => prices.Min() + 10 * i)
                .TakeWhile(i => i < Math.Min(100, prices.Max()))
                .Concat(
                    Enumerable
                    .Range(1, 1000)
                    .Select(i => 100 * i)
                    .TakeWhile(i => i <= prices.Max())
                ).ToList();
            Console.WriteLine(string.Join(", ", finalPriceList));
        }
    }
}

打印出来:

10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400

【讨论】:

    【解决方案2】:

    您是否要生成“n”个元素或是否要生成直到列表中的最大值,有点不清楚。

    案例1:如果在列表中生成直到最大值

    public IEnumerable<int> Generate(int maxValue)
    {
        var currentValue = 0;
        while(currentValue<maxValue)
        {
            currentValue += currentValue < 100 ? 10:100;
            yield return currentValue;
        }
    }
    

    您现在可以将方法用作

    var myList = new List<int>(new int[] { 12, 21, 30, 90, 100, 150, 404 });
    var result = Generate(myList.Max());
    

    案例2:当你想生成N个元素时。

    var initialValue = 0;
    var list = Enumerable.Range(1,totalNumberOfExpectedItems)
                         .Select(x=> 
                           { 
                              initialValue = initialValue < 100 ? initialValue + 10 : initialValue + 100; 
                              return initialValue;
                           })
    

    Enumerable.Range 帮助生成指定范围内的序列

    对于totalNumberOfExpectedItems = 13

    输出

    10 
    20 
    30 
    40 
    50 
    60 
    70 
    80 
    90 
    100 
    200 
    300 
    400 
    

    【讨论】:

      【解决方案3】:

      试试这个...

      我假设您正在获取列表中最低和最高值之间的所有值。

      List<int> myList = new List<int>(new int[] { 12, 21, 30, 90, 100, 150, 404 });
      int max = myList.Max() - myList.Max() % 100;
      int min = myList.Min() - myList.Min() % 10;
      
      int tmp = (int)Math.Round(Math.Log10(min), MidpointRounding.ToZero);
      List<int> newList = new List<int>();
      for (int a = tmp, b = min / (int)Math.Pow(10, tmp); (Math.Pow(10, a) * b <= max); )
      {
           newList.Add((int)(Math.Pow(10, a) * b));
           if (b < 9) b++;
           else
           {
               b = 1;
               a++;
           }
      }
      
      //newList : [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-27
        • 1970-01-01
        • 2019-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多