选择投影中的索引 [英] Index in the Select projection

查看:125
本文介绍了选择投影中的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行以下操作时,我希望索引从大于0的数字开始:

I would like my index to start from a number count greater than 0 while doing something like this:

var dataSource = WebConfigurationHelper.GetSupportedDomainsString().Select((domain, index) => 
new { index , Name = domain });

所以我的输出变为:

index=2 domain=zombieland
index=3 domain=mydomain

有可能吗?

推荐答案

您可以在选择"投影中对其进行调整:

You can adjust it in the Select projection:

var dataSource = WebConfigurationHelper.GetSupportedDomainsString()
     .Select((domain, index) =>  new { Index = index + 2, Name = domain });

我最初对new { index + 2, Name = domain }的建议不起作用的原因是 projection initializer (您只指定一个表达式,然后让编译器推断名称)仅在该表达式为简单名称",成员访问权限"或基本访问权限".

The reason my original suggestion of new { index + 2, Name = domain } wouldn't work is that a projection initializer (where you specify just an expression, and let the compiler infer the name) only works when the expression is a "simple name", a "member access" or a "base access".

这篇关于选择投影中的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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