Select 投影中的索引 [英] Index in the Select projection
本文介绍了Select 投影中的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在执行以下操作时,我希望我的索引从大于 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
可以吗?
推荐答案
可以在Select投影中进行调整:
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".
这篇关于Select 投影中的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文