你如何索引字段添加到Linq的结果 [英] How do you add an index field to Linq results
本文介绍了你如何索引字段添加到Linq的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以说我有一个这样的数组:
Lets say I have an array like this:
string [] Filelist = ...
我想创建一个Linq的结果,其中的每一项都有是这样的数组中的位置是:
I want to create an Linq result where each entry has it's position in the array like this:
var list = from f in Filelist
select new { Index = (something), Filename = f};
指数为第1项,1为第二等为0。
Index to be 0 for the 1st item, 1 for the 2nd, etc.
我应该使用什么为前pression指数=?
What should I use for the expression Index= ?
推荐答案
不要使用查询前pression。使用 选择
其中传递您的索引<超负荷/ A>
Don't use a query expression. Use the overload of Select
which passes you an index:
var list = FileList.Select((file, index) => new { Index=index, Filename=file });
这篇关于你如何索引字段添加到Linq的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文