Iteration through 1 list compiled of multiple sub lists?

Ryan Ragan
Ryan Ragan used Ask the Experts™
on
Good morning, I have a quick question about lists. I have multiple List(of FileInfo) that searches for employee paperwork. Why? I need to know what files are turned in and then iterate through the schedule database to find missing paperwork (check days worked vs paperwork turned in (Dates) to find information). Problem: If I create a list for each employee that holds the missing paperwork dates, can I place each list into 1 container and then iterate through the big container to retrieve those dates to generate a report or am I making this too complicated??? Each Date in each container goes to a specific listbox so I can see individually who is turning in late work. Any advice would be great, thank you!


                   
Dim Provider1 As New List(Of FileInfo)
                    Dim Provider2 As New List(Of FileInfo)
                    Dim Provider3 As New List(Of FileInfo)
                    Dim Provider4 As New List(Of FileInfo)
                    Dim Provider5 As New List(Of FileInfo)
                    Dim Provider6 As New List(Of FileInfo)
                    Dim Provider7 As New List(Of FileInfo)
                    Dim Provider8 As New List(Of FileInfo)
                    Dim Provider9 As New List(Of FileInfo)
                    Dim Provider10 As New List(Of FileInfo)
                    Dim NewFileInfo As New FileInfo(newname)
                    For Each Item In DirectoryList ' container holds directory information for each employee file collections
                        Dim di11 As New IO.DirectoryInfo(Item.ToString)
                        Dim diar111 As IO.FileInfo() = di11.GetFiles("*.pdf")
                        Dim dra11 As IO.FileInfo
                        For Each dra11 In diar111
                            If NewFileInfo.Exists = False And Provider = "Provider1" Then Provider1.Add(NewFileInfo)
                            If NewFileInfo.Exists = False And Provider = "Provider2" Then Provider2.Add(NewFileInfo)
                            If NewFileInfo.Exists = False And Provider = "Provider3" Then Provider3.Add(NewFileInfo)
                            If NewFileInfo.Exists = False And Provider = "Provider4" Then Provider4.Add(NewFileInfo)
                            If NewFileInfo.Exists = False And Provider = "Provider5" Then Provider5.Add(NewFileInfo)
                            If NewFileInfo.Exists = False And Provider = "Provider6" Then Provider6.Add(NewFileInfo)
                            If NewFileInfo.Exists = False And Provider = "Provider7" Then Provider7.Add(NewFileInfo)
                            If NewFileInfo.Exists = False And Provider = "Provider8" Then Provider8.Add(NewFileInfo)
                            If NewFileInfo.Exists = False And Provider = "Provider9" Then Provider9.Add(NewFileInfo)
                            If NewFileInfo.Exists = False And Provider = "Provider10" Then Provider10.Add(NewFileInfo)
                        Next
                    Next

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dustin SaundersCo-Founder and Chief Architect
Top Expert 2016

Commented:
I don't do VB so much these days, but that's generally what you would do to load data into those lists.  You can simplify your code quite a bit with methods,  but I'd only be able to do a sample in C#.

For ListBoxes that contain specific data, I generally make a new class that inherits ListBox and handle the data within that class.  Quick example:
    class ProviderListBox : System.Windows.Forms.ListBox
    {
        public ProviderListBox(int providerNumber, List<System.IO.FileInfo> dataSource)
        {
            _providerNumber = providerNumber;
            this.DataSource = dataSource.Where(ds => !ds.Exists && ds.Provider = this.ProviderString).ToList();
        }

        private int _providerNumber;

        public int ProviderNumber { get { return _providerNumber; } }
        public string ProviderString { get { return "Provider" + _providerNumber.ToString(); } }
    }

Open in new window


You could reference the actual parent list from this class, if you want to have changes in individual boxes tethered to a 'master list'.

Take a look at LINQ Queries as well, as that makes working with data in code much easier than conditional splits (and if using Entity Framework you can pull data much more efficiently just from code).
You can use dictionary
        Dim providers As New Dictionary(Of String, List(Of IO.FileInfo))
        For i = 1 To 10
            providers.Add("provider" & i.ToString, New List(Of IO.FileInfo))
        Next
'.....................
        For Each dra11 In diar111
            If Not NewFileInfo.Exists Then providers.Add(provider, NewFileInfo)
        Next

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial