C#LINQ表达式问题 [英] C# LINQ expression question

查看:65
本文介绍了C#LINQ表达式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例代码:

string[] digits = { "zero", "one", "two", "three", 
  "four", "five", "six", "seven", "eight", "nine" };

var shortDigits = digits.Where((digit, index) => digit.Length < index);

Console.WriteLine("Short digits:");
foreach (var d in shortDigits)
{
    Console.WriteLine("The word {0} is shorter than its value.", d);
}

是否有一种使用LINQ表达式仅生成 相同输出的方法?

Is there a way generating same output only by using LINQ expressions?

推荐答案

我了解您仅指的是var shortDigits = digits.Where((digit, index) => digit.Length < index);我必须认真思考为什么要创建这样的怪物,如果您指的是生成仅LINQ

I understand you are referring only to var shortDigits = digits.Where((digit, index) => digit.Length < index); I would have to think hard why you'd want to create such a monster if you are referring to generating the whole output with only LINQ

您可以执行以下操作以获得相同的输出:

You could do the following to get the same output:

int i = 0;
var shortDigits = from d in digits
                  where d.Length < i++
                  select d;

这篇关于C#LINQ表达式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆