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

发布于 2024-12-05 08:15:19 字数 1275 浏览 2 评论 0原文

是否可以在 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 的 Selects 中获取当前的“索引”(计数器?)?我想在 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长梦不多时 2024-12-12 08:15:20

是的 - 您只需要使用 重载 Select 即可采用 Func。所以在 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 中,但它更有可能以这种方式工作 - 希望你能完成翻译:)

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

扛刀软妹 2024-12-12 08:15:20

@V-Light,

您可以执行类似的操作来获取迭代器索引:

XDocument xml = new XDocument(New XElement("Items",  from item in someItemsResourses where item.FullName.length => 5 orderby item descending  //Get   Iterator Index here and make use of it as you wish  let currIndex = someItemsResourses.IndexOf(item)  select new XElement("item", new XAttribute("id", ?how?), item.FullName)))

@V-Light,

You could do something like this to get your iterator index:

XDocument xml = new XDocument(New XElement("Items",  from item in someItemsResourses where item.FullName.length => 5 orderby item descending  //Get   Iterator Index here and make use of it as you wish  let currIndex = someItemsResourses.IndexOf(item)  select new XElement("item", new XAttribute("id", ?how?), item.FullName)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文