简体   繁体   English

如何从 Ienumerable 返回一个字符串?

[英]How can I return a string from an Ienumerable?

I am very new to programming and am taking an Object Oriented Programming class. However, the professor didn't explain how to take an Ienumerable and make it into a string in order to accomplish this question of the assignment:我是编程新手,正在上Object Oriented Programming class。但是,教授没有解释如何将Ienumerable变成一个字符串来完成这个作业题:

TODO: Write a public static C# method named NumSquare that takes a one-dimentional array as input and creates a LINQ statement that queries the numbers that have a square number graeter than 20 and orders them ascending. TODO:编写一个名为 NumSquare 的公共 static C# 方法,该方法采用一维数组作为输入并创建一个 LINQ 语句来查询平方数大于 20 的数字并对其进行升序排序。 The LINQ query retrieves anonymous objects in which each object contains the number (Num) and its square number (SqrNum). LINQ 查询检索匿名对象,其中每个 object 包含数字 (Num) 及其平方数 (SqrNum)。 The method returns the LINQ query as an IEnumerable object. The anonymous object contains two instance variables named Num and SqrNum.该方法将 LINQ 查询作为 IEnumerable object 返回。匿名 object 包含两个名为 Num 和 SqrNum 的实例变量。 Input: a one-dimentional integer array.输入:一维 integer 数组。 Output: a LINQ query of type IEnumerable. Output:IEnumerable 类型的 LINQ 查询。 Example: Given array A = [3, 4, 10, 5], invoking NumSquare(A) return a LINQ query that once executed will contain: {Num=5, SqrNum=25}, {Num=10, SqrNum=25}示例:给定数组 A = [3, 4, 10, 5],调用 NumSquare(A) 返回一个 LINQ 查询,一旦执行将包含:{Num=5, SqrNum=25}, {Num=10, SqrNum=25}

Here's what I have so far, but I've tried several things over the last 2 1/2 weeks.到目前为止,这是我所拥有的,但在过去的 2 1/2 周内,我已经尝试了几件事。

        public static IEnumerable<object> NumSquare(int[] A)
        {
            //write your code here

            var num = from Number in A
                      select Number;
            var sqrnum = from Number in A
                         let squarenum = Number * Number
                         select squarenum;
            
            return (IEnumerable<object>)sqrnum;
        }

I know that this return won't get me the whole result that I need, but that's as far as I can get with no errors.我知道这个回报不会让我得到我需要的全部结果,但这是我能得到的,没有错误。 I also don't know how to test anything because he didn't show us how to call an IEnumerable.我也不知道如何测试任何东西,因为他没有向我们展示如何调用 IEnumerable。 Help?帮助?

I think what you are looking for is not a string as output but as the exercise says an anonymous object. An anonymous object can be something like this:我认为您正在寻找的不是字符串 output,而是练习中所说的匿名 object。匿名 object 可以是这样的:

var o = new { Num = 4, SqrNum = 16 };

Its just an object that basically has no explicit type and some read-only variables.它只是一个 object,基本上没有显式类型和一些只读变量。

So what you want to do is to convert your array into a IEnumerable<{int Num, int SqrNum}> which you would have to declare as IEnumerable<object> and not a string.因此,您想要做的是将数组转换为IEnumerable<{int Num, int SqrNum}> ,您必须将其声明为IEnumerable<object>而不是字符串。

You could do something like this:你可以这样做:

static IEnumerable<object> NumSqr(int[] a)
{
    return a
        .Where(x => x * x > 20)
        .OrderBy(x => x)
        .Select(x => new { Num = x, SqrNum= x * x });
}

Alternatively:或者:

static IEnumerable<object> NumSqr(int[] a)
{
    return from number in a
        where number * number > 20
        orderby number
        select new { Num = number, SqrNum = number * number };
}

In order to print out the result of the function you could do this:为了打印出 function 的结果,您可以这样做:

var a = new int[] { 3, 4, 10, 5 };
var result = NumSqr(a);
foreach (var obj in result)
{
    Console.WriteLine(obj);
}

The output should look like this: output 应如下所示:

{ Num = 5, SqrNum = 25 }
{ Num = 10, SqrNum = 100 }

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

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