Is that possible to get current Enumerator (...or iterator? Don't know which tern is the correct one) in a LINQ query ?
For example, I try to create a XML output (via LINQ to XML) of all currently loaded assemblies.
Dim xmldoc As XDocument = New XDocument(
New XElement("Types",
Assembly.GetExecutingAssembly().GetReferencedAssemblies() _
.Select(Function(name) Assembly.Load(name)) _
.SelectMany(Function(assembly) assembly.GetTypes()) _
.Select(Function(type) New XElement("Type", type.FullName))))
Tho output looks like this.
<Types>
<Type>System.Object</Type>
<Type>FXAssembly</Type>
<Type>ThisAssembly</Type>
<Type>AssemblyRef</Type>
<Type>System.Runtime.Serialization.ISerializable</Type>
<Type>System.Runtime.InteropServices._Exception</Type>
.....
</Types>
is it possible to somehow get current "index" (counter?) from LINQ's Selects? I would like to use it in XML
<Types>
<Type ID="1">System.Object</Type>
<Type ID="2">FXAssembly</Type>
<Type ID="3">ThisAssembly</Type>
<Type ID="4">AssemblyRef</Type>
<Type ID="or-some-other-unique-id-#5">System.Runtime.Serialization.ISerializable</Type>
.....
</Types>
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))));
Sorry it's not in VB, but it's more likely to work this way - hopefully you can work out the translation :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With