加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

.net – 在LINQ查询中获取当前枚举数(iterator?).就像for循环中

发布时间:2020-12-16 23:47:55 所属栏目:百科 来源:网络整理
导读:是否有可能在LINQ查询中获取当前的枚举器(…或迭代器?不知道哪个是正确的)? 例如,我尝试创建所有当前加载的程序集的XML输出(通过LINQ to XML). Dim xmldoc As XDocument = New XDocument( New XElement("Types",Assembly.GetExecutingAssembly().GetRefere
是否有可能在LINQ查询中获取当前的枚举器(…或迭代器?不知道哪个是正确的)?

例如,我尝试创建所有当前加载的程序集的XML输出(通过LINQ to XML).

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))))

输出看起来像这样.

<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>

是否有可能以某种方式从LINQ的选择中获得当前的“索引”(计数器?)?我想在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>
是的 – 你只需要使用 overload of Select which takes a Func<TSource,int,TResult>.所以在C#中它会是这样的:
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中,但它更有可能以这种方式工作 – 希望你能解决这个问题:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读