Question

My working data is a list of type string:

    a.aspx?a=1
    a.aspx?a=2
    a.aspx?a=3
    b.aspx?b=1
    b.aspx?b=2
    b.aspx?b=3
    c.aspx?c=1
    c.aspx?c=2
    c.aspx?c=3

What i'd like to do is:

  1. take the first value in the list (a.aspx?a=1)
  2. get the pagename (a.aspx)
  3. place all the matching pages into a separate collection (a.aspx?a=1,2,3)
  4. remove all the entries in the original list that match the pagename (a.aspx)
  5. Repeat 1-4, this time the first entry will be b.aspx
  6. So on until i have separate collections for each page name

I can do 1-3 alright but i'm not sure the best way to do step 4 & 5 namely dynamically creating the collections from within a loop or some alternative that achieves a similar result.

Any suggestions?

Was it helpful?

Solution

So you want to GroupBy page name and build sub-lists for every page? Then this should work:

List<List<string>> urlsPerName = list
    .Select(u => new { Name = Path.GetFileNameWithoutExtension(u), Full = u })
    .GroupBy(x => x.Name)
    .Select(g => g.Select(x => x.Full).ToList())
    .ToList();

Result, three lists with three strings each:

        [0] "a.aspx?a=1"    string
        [1] "a.aspx?a=2"    string
        [2] "a.aspx?a=3"    string

        [0] "b.aspx?b=1"    string
        [1] "b.aspx?b=2"    string
        [2] "b.aspx?b=3"    string

        [0] "c.aspx?c=1"    string
        [1] "c.aspx?c=2"    string
        [2] "c.aspx?c=3"    string
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top