vb.net - 将多个名为 XX 的文件插入字符串中

发布于 2024-10-26 16:46:16 字数 889 浏览 1 评论 0原文

使用以下代码创建文件夹中所有图像的表格。

VB:

Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
      Dim dirInfo as New DirectoryInfo(Server.MapPath("/images"))
      articleList.DataSource = dirInfo.GetFiles("*.jpg")
      articleList.DataBind()
    End If
  End Sub

正文:

<asp:DataGrid runat="server" id="articleList" AutoGenerateColumns="False" ShowHeader="false">
    <Columns>
      <asp:BoundColumn DataField="Name" />
    </Columns>
  </asp:DataGrid>

结果如下所示:

aa_01.jpg
aa_02.jpg
aa_03.jpg
bb_01.jpg
bb_02.jpg
cc_01.jpg
cc_02.jpg
cc_03.jpg
cc_04.jpg
...

我现在想做的是将所有这些按前两个字符分组,并获取每个字符的总数并将它们插入到单独的字符串中。因此,对于上面的示例,它会类似于:

Dim aa As String = 3
暗淡 bb 作为字符串 = 2
Dim cc As String = 4

有什么想法吗?

Have the following code which creates a table of all the images in a folder.

VB:

Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
      Dim dirInfo as New DirectoryInfo(Server.MapPath("/images"))
      articleList.DataSource = dirInfo.GetFiles("*.jpg")
      articleList.DataBind()
    End If
  End Sub

Body:

<asp:DataGrid runat="server" id="articleList" AutoGenerateColumns="False" ShowHeader="false">
    <Columns>
      <asp:BoundColumn DataField="Name" />
    </Columns>
  </asp:DataGrid>

The results look something like this:

aa_01.jpg
aa_02.jpg
aa_03.jpg
bb_01.jpg
bb_02.jpg
cc_01.jpg
cc_02.jpg
cc_03.jpg
cc_04.jpg
...

What I want to do now is group all these by the first two characters and get the total number for each and insert them into individual strings. So for the above example it would be something like:

Dim aa As String = 3
Dim bb As String = 2
Dim cc As String = 4

Any ideas how?

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

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

发布评论

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

评论(2

森末i 2024-11-02 16:46:16

您可以使用 Linq 来完成此操作。试试这个。

dim q = From p in articles _
        Group By Name = p.Name.SubString(0,2) into g = group _
        Select GroupName = Name, _
               Count = g.Count()

更新(根据评论提供一些背景信息)

Sub Page_Load(sender as Object, e as EventArgs)

    If Not Page.IsPostBack then
      Dim dirInfo as New DirectoryInfo(Server.MapPath("/images"))
      Dim articles = dirInfo.GetFiles("*.jpg")

      articleList.DataSource = articles
      articleList.DataBind()

      Dim groupings = From p in articles _
                      Group By Name = p.Name.SubString(0,2) into g = group _
                      Select GroupName = Name, _
                             Count = g.Count()

      ' do whatever you like with the groupings '
    End If

End Sub

You can do this using Linq. Try this.

dim q = From p in articles _
        Group By Name = p.Name.SubString(0,2) into g = group _
        Select GroupName = Name, _
               Count = g.Count()

Update (to give a bit of context as per comments)

Sub Page_Load(sender as Object, e as EventArgs)

    If Not Page.IsPostBack then
      Dim dirInfo as New DirectoryInfo(Server.MapPath("/images"))
      Dim articles = dirInfo.GetFiles("*.jpg")

      articleList.DataSource = articles
      articleList.DataBind()

      Dim groupings = From p in articles _
                      Group By Name = p.Name.SubString(0,2) into g = group _
                      Select GroupName = Name, _
                             Count = g.Count()

      ' do whatever you like with the groupings '
    End If

End Sub
落花浅忆 2024-11-02 16:46:16

在 C# 中,使用 LINQ:

groupedArticleList = (from a in articleList
                      group a by a.Name.Substring(0, 2) into g
                      select new
                      {
                          GroupName = g.Key,
                          ArticleCount = g.Count()
                      });

您无法真正将它们提取到单独的变量中,但您可以按原样使用 groupedArticleList 并循环遍历它或将其转换为 Dictionary< /代码>。

抱歉,我不知道 VB 语法,但希望这会有所帮助。

In C#, using LINQ:

groupedArticleList = (from a in articleList
                      group a by a.Name.Substring(0, 2) into g
                      select new
                      {
                          GroupName = g.Key,
                          ArticleCount = g.Count()
                      });

You can't really extract them into separate variables but you could use groupedArticleList as-is and loop through it or convert it to a Dictionary<string, int>.

Sorry I don't know the VB syntax but hopefully that will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文