【问题标题】:How the transformation is possible?转型怎么可能?
【发布时间】:2009-10-16 16:34:32
【问题描述】:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
string[] strings = { "zero", "one", "two", "three", "four", "five", "six",
                     "seven","eight", "nine" };

 var textNums =
                from n in numbers
                select strings[n];

   Console.WriteLine("Number strings:");

   foreach (var s in textNums)
   {
                Console.WriteLine(s);
   }

1) 将“整数”转换为表示“单词”中的整数的机制是什么?

2) 只有 int 到 string 才有可能进行这种转换?或者我们可以一起玩吗 这种转变?

【问题讨论】:

    标签: c# linq-to-objects transform


    【解决方案1】:
    1. 这只是数组访问 - 它使用“数字”中的元素作为“字符串”数组的索引。

    2. 只有整数适用于数组,但您同样可以使用Dictionary<string, string> 或其他任何东西来进行任意映射。在这种情况下,您可以将字符串数组视为Dictionary<int, string>。你也可以这样重写:

      int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
      var words = new Dictionary<int, string>
      {
          { 0, "zero" },
          { 1, "one" },
          { 2, "two" },
          { 3, "three" },
          { 4, "four" },
          { 5, "five" },
          { 6, "six" },
          { 7, "seven" },
          { 8, "eight" },
          { 9, "nine" }
      };
      var textNums = from n in numbers
                     select words[n];
      

      Console.WriteLine("数字字符串:");

      foreach(textNums 中的 var s) { Console.WriteLine(s); }

    这仍然使用整数 - 但您可以对键是其他类型的字典执行相同的操作。

    【讨论】:

    • 非常感谢乔恩。 :) 祝你有美好的一天
    • 我期待您对我之前的一个问题(问题标题:Linq- Running Total and Sub Total)的回答。如果时间允许,请帮助我。
    • 乔恩,我认为这不是最好的解决方案。哦,我说了什么!?!
    • @Tony:这是对他如何从整数版本到非整数版本的解释。它适用于作为字典中的键的任意类型 - 而枚举不会。
    • @linqfying:这个问题的答案已经非常好了。我看不出我还要添加什么。
    【解决方案2】:

    没有。字符串表示只是以正确的顺序而已。这里没有魔法。

    查看字符串数组

    strings[0] = "zero";
    strings[1] = "one";
    strings[2] = "two";
    .
    .
    

    正确排序的事实是映射起作用的原因。

    【讨论】:

    • 非常感谢。我为自己的愚蠢感到难过。我没有正确地完成这个例子。
    【解决方案3】:

    当您说 strings[n] 时,您正在访问数组的第 n 个值,并且数组的顺序如下:

    字符串[0] = "零"; 字符串[1] =“一个”; ... 字符串[4] = "四个";

    所以,这里没有魔法,只是一个有序数组:P

    【讨论】:

    • 对不起!我没有正确完成它。谢谢。 :)
    【解决方案4】:

    我会做以下事情:

    public enum MyNumberType { 
            Zero = 0, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
            }
    

    你可以通过以下方式做你想做的事:

    namespace ConsoleApplication
    {
        class Program
        {
            public enum MyNumberType { Zero = 0, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten }
    
            private static int GetIntValue(MyNumberType theType) { return (int) theType; }
            private static String GetStringValue(MyNumberType theType) { return Enum.GetName(typeof (MyNumberType),theType); }
            private static MyNumberType GetEnumValue (int theInt) {
                return (MyNumberType) Enum.Parse( typeof(MyNumberType), theInt.ToString() ); }
    
            static void Main(string[] args)
            {
                Console.WriteLine( "{0} {1} {2}", 
                    GetIntValue(MyNumberType.Five), 
                    GetStringValue( MyNumberType.Three),
                    GetEnumValue(7)
                    );
                for (int i=0; i<=10; i++)
                {
                    Console.WriteLine("{0}", GetEnumValue(i));
                }
            }
        }
    }
    

    产生以下输出:

    5 Three Seven
    Zero
    One
    Two
    Three
    Four
    Five
    Six
    Seven
    Eight
    Nine
    Ten
    

    这可以扩展到更大的数字和不在连续范围内的数字,如下所示:

    public enum MyNumberType { 
            ten= 10, Fifty=50, Hundred=100, Thousand=1000
            }
    

    枚举不仅可以与 int 类型一起使用,还可以与其他类型一起使用,因此非常灵活。

    【讨论】:

    • 此解决方案的缺点是您将代码同时视为代码和文本。一个名为 "Three" 的枚举可以转换为字符串这一事实在很多地方都很有用,但我觉得在用户输出中实际将名称视为字符串是相当尴尬的。
    • 此外,它不适用于非整数类型(例如字符串、浮点数、Guids),并且不容易国际化(不像字典,你可以为不同的语言提供不同的字典)。
    • 他实际上并没有说他想用它来烤吐司或确实在国际上工作......尽管他可能......他不喜欢吐司我喜欢吐司。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 2013-07-14
    • 2011-01-31
    相关资源
    最近更新 更多