C# 使用LINQ打印阵列

C# 使用LINQ打印阵列,c#,linq,C#,Linq,我得到了下面的代码,我正在尝试打印青少年学生,而不使用我在LINQ行之后制作的foreach。 我可以在LINQ行中添加打印吗? 我可以用另一条新的LINQ线代替foreach打印吗 class Student { public int StudentID { get; set; } public string StudentName { get; set; } public int Age { get; set; } } class Program { stat

我得到了下面的代码,我正在尝试打印
青少年学生
,而不使用我在LINQ行之后制作的foreach。 我可以在LINQ行中添加打印吗? 我可以用另一条新的LINQ线代替foreach打印吗

class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public int Age { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        Student[] studentArray = {
                new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
                new Student() { StudentID = 2, StudentName = "Steve",  Age = 21 } ,
                new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
                new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
                new Student() { StudentID = 5, StudentName = "Ron" , Age = 31 } ,
                new Student() { StudentID = 6, StudentName = "Chris",  Age = 17 } ,
                new Student() { StudentID = 7, StudentName = "Rob",Age = 19  } ,
    };
        Student[] teenAgerStudents = studentArray.Where(s => s.Age > 12 && s.Age < 20).ToArray();

        foreach (var item in teenAgerStudents)
        {
            Console.WriteLine(item.StudentName);
        }
};
班级学生
{
公共int StudentID{get;set;}
公共字符串StudentName{get;set;}
公共整数{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
学生[]学生阵列={
new Student(){StudentID=1,StudentName=“John”,年龄=18},
新学生(){StudentID=2,StudentName=“Steve”,年龄=21},
新学生(){StudentID=3,StudentName=“Bill”,年龄=25},
新学生(){StudentID=4,StudentName=“Ram”,年龄=20},
新学生(){StudentID=5,StudentName=“Ron”,年龄=31},
new Student(){StudentID=6,StudentName=“Chris”,年龄=17},
new Student(){StudentID=7,StudentName=“Rob”,年龄=19},
};
学生[]青少年学生=学生阵列。其中(s=>s.Age>12和s.Age<20)。ToArray();
foreach(青少年学生中的变量项)
{
Console.WriteLine(item.StudentName);
}
};
这将起作用:

studentArray.Where(s => s.Age > 12 && s.Age < 20)
            .ToList()
            .ForEach(s => Console.WriteLine(item.StudentName));
但这会降低它的可读性(在我看来),因此在这种情况下,我会坚持使用一个好的
foreach
循环。

我不喜欢使用LINQ(一个函数式编程系统)来产生副作用。我宁愿将foreach的输出更改为这样的内容:

Console.WriteLine(string.Join(Environment.NewLine, 
    teenAgerStudents.Select(s => s.StudentName));
试试这个:

  // There's no need in materialization, i.e. "ToArray()"
  var target = studentArray 
    .Where(student => student.Age > 12 && student.Age < 20) // teen
    .Select(student => String.Format("Id: {0} Name: {1} Age {2}", 
              student.Id, student.Name, student.Age));

  // Printing out in one line (thanks to String.Join)
  Console.Write(String.Join(Environment.NewLine, target));
//不需要物化,即“ToArray()”
var目标=学生阵列
.Where(student=>student.Age>12&&student.Age<20)//青少年
.Select(student=>String.Format(“Id:{0}名称:{1}年龄{2}”),
学生Id、学生姓名、学生年龄);
//一行打印出来(感谢String.Join)
Write(String.Join(Environment.NewLine,target));

此行将在控制台上打印您的青少年学生姓名,每行一个

Console.Write(string.Join(Environment.NewLine, studentArray.Where(s => s.Age > 12 && s.Age < 20).Select(s=>s.StudentName)));

希望这能有所帮助。

是的,你可以。对于简单的情况也可以。但是,LINQ的性能通常比编写代码差得多。我倾向于尽可能避免使用它,除非我绝对确定性能不是,也永远不会成为一个因素。在第一种情况下,只需删除ToArray()-它没有添加任何内容。@Zastai这不起作用,
ForEach
没有为
IEnumerable
My bad定义。当我在手机上发表评论时,会发生这种情况,无法快速查看文档。第二个代码正是我要查找的,但它不起作用,有一条红线(错误)在ForEach@AlexanderDerck评论不是写给你的:)你的答案很好(如果你用
s
替换
item
:)这是一个非常独特的方法。突出显示了一个事实,即
target
尚未执行,并且将在真正需要时执行。+1。但会收集完整的结果集,浪费内存。流式处理结果更具可伸缩性。@Zastai:这取决于数据的大小。因为数据表示为数组(所以整个数据都在内存中)我假设我们有足够的内存来存储
字符串。Join
(如果我们从文件中读取,流式传输将是一个点)问题中的示例代码通常只是一个示例;我试着不太依赖它。但是,使用这个数组是可以的。
Console.Write(string.Join(Environment.NewLine, studentArray.Where(s => s.Age > 12 && s.Age < 20).Select(s=>s.StudentName)));
//get the teenager students in form of IEnumerable<Student>.
//cast it to array with .ToArray() after .Where() if needed.
var teenAgerStudents = studentArray.Where(s => s.Age > 12 && s.Age < 20);
//print only the students' names
Console.Write(string.Join(Environment.NewLine,teenAgerStudents.Select(s=>s.StudentName)));