且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在LINQ查询中获取当前的枚举数(迭代器?).就像for循环中的当前索引

更新时间:2022-04-15 02:36:58

是的-您只需要使用 Select 的重载,该重载采用 Func< TSource,int,TResult> .因此,在C#中,它将类似于:

Yup - you just need to use the overload of Select which takes a Func<TSource, int, TResult>. So in C# it would be something like:

XDocument doc = new XDocument(new XElement("Types",
    Assembly.GetExecutingAssembly().GetReferencedAssemblies()
        .Select(name => Assembly.Load(name))
        .SelectMany(assembly => assembly.GetTypes())
        .Select((type, index) => new XElement("Type",
                                              new XAttribute("ID", index + 1), 
                                              type.FullName))));

对不起,它不在VB中,但更可能以这种方式工作-希望您能完成翻译工作:)

Sorry it's not in VB, but it's more likely to work this way - hopefully you can work out the translation :)