1

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?

Tom
  • 12,776
  • 48
  • 145
  • 240

2 Answers2

1

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
Geoff Appleford
  • 18,538
  • 4
  • 62
  • 85
  • Sounds like that could be what I'm looking for but where do I place this in the above example?? – Tom Mar 24 '11 at 15:22
  • Added this: `For Each s In groupings Response.write(groupings) Response.write("
    ") Next s` - Each result was **System.Linq.Enumerable+WhereSelectEnumerableIterator`2[VB$AnonymousType_0`2[System.String,System.Collections.Generic.IEnumerable`1[System.IO.FileInfo]],VB$AnonymousType_1`2[System.String,System.Int32]]** What's going on there?
    – Tom Mar 24 '11 at 16:54
  • @Tom - you want to do something like `For Each s In groupings Response.write("Name = " & s.GroupName & " Count = " & s.Count) Response.write("
    ") Next s` - groupings is an `IEnumerable` of anonymousTypes where each object has a GroupName and Count property. You could also bind it to a datagrid like you have done with `articleList` where the columns `DataField`s would be `GroupName` and `Count`
    – Geoff Appleford Mar 24 '11 at 17:05
0

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.

Josh M.
  • 26,437
  • 24
  • 119
  • 200
  • I think the 1 argument overload of substring specifies the startindex rather than the number of characters. See http://msdn.microsoft.com/en-us/library/hxthx5h6.aspx – Geoff Appleford Mar 24 '11 at 15:14