Kraz Kraz - 5 months ago 10
Vb.net Question

How to regroup what's under a threshold with linq?

Say I have a list of string with specific names :

APP_application1.exe
APP_application2.exe
APP_application3.exe
APP_application4.exe
CMD_Batch.exe
PWS_Script.exe
PWS_Script2.exe
PWS_Script3.exe
VBS_Script.exe
VBS_Script2.exe
[...]


I want to regroup those with the same prefix together but only if they are over a threshold. Those under that limit are bundled together in an "other" tag. I also want a maximum of 5 groups.

Group APP : {APP_application1.exe,APP_application2.exe,APP_application3.exe,APP_application4.exe}
Group PWS : {PWS_Script.exe,PWS_Script2.exe,PWS_Script3.exe}
Group other : {CMD_Batch.exe,VBS_Script.exe,VBS_Script2.exe}


Right now, I have hardcoded my groups, so I can filter with a prefix

filtered = _
(From cell In listFiles _
Where cell.ToUpper().StartsWith(prefix)
Select New SelectListItem() With { _
.Text = cell, _
.Value = cell _
}).ToList()


Or filter with those outside it :

filtered = _
(From cell In listFiles _
Let prefix = cell.Substring(0, 3).ToUpper()
Where Not LIST_CODE.Contains(prefix)
Select New SelectListItem() With { _
.Text = cell, _
.Value = cell _
}).ToList()


Where
LISTE_CODE = {"APP","PWS"}


I want to get rid of
LISTE_CODE
and use something like
MIN_GROUP_SIZE = 3
and
MAX_GROUP = 5
, to be more dynamic.

Right now I have

Dim grp =
(From cell In listFiles _
Let prefixe = cell.Substring(0.3).ToUpper()
Group By pre = prefixe Into g = Group
Where g.Count() < MIN_GROUP_SIZE
Select g)


But it doesn't group at all.

Do I have the right approach?

What's wrong with my
group by
?

Answer

I ended up doing it in 2 parts :

        Dim listGroup = From listeCmd In listFiles
                     Let prefixe = listeCmd.Substring(0, 3).ToUpper()
                     Group By prefix Into Group
                     Order By Group.Count() Descending

        For Each item In listGroupe.Where(Function(e) e.Group.Count > Me.MIN_PER_PREFIX)
            If listB.Count < Me.MAX_GROUP Then
                listB.Add(item.prefix)
            End If
        Next

This gave me my list of group's name, which I used to replace my LIST_CODE const.