0

Given the below code I want to be able to find an element in the myProjectsDicList using a key (string) in a different dictionary. But look for it in projectsArrayList array. Specifically the htmleditor.ProjectName string is what I want to find. There is only 1 List<ArrayList> in the dictionary. This array list contains the projects array list.

I tried this below but doesn't work....

string key = "Project Name";
var match = myProjectsDicList.Where(item => item.Value[0].Cast<object>().Where(x => x.ProjectName == key).Select(x => x.ProjectName).First());

I'm trying to find a way to replace this code to find the HTMLEditor that works with a select statement.

            bool foundHTMLEditor = false;
            for (int i = 0; i < myProjectsDicList.Count; i++)
            {
                List<ArrayList> allprojects = myProjectsDicList.ElementAt(i).Value;

                for (int j = 0; j < allprojects[0].Count; j++)
                {
                    HTMLEditor e = (HTMLEditor)allprojects[0][j];

                    if (e.ProjectName == key)
                    {
                        foundHTMLEditor = true;
                        break;
                    }
                }

                if(foundHTMLEditor == true)
                {
                    break;
                }
            }

Here is the code I have...

public class MultiDimDictList : Dictionary<string, List<ArrayList>>
{
}

projects = new ArrayList();
List<ArrayList> projectsArrayList = new List<ArrayList>();

// create the dictionary
MultiDimDictList myProjectsDicList = new MultiDimDictList();

// create an htmleditor instance and set one of the variables
HTMLEditor htmleditor = new HTMLEditor();
htmleditor.ProjectName = "Project Name";

// add the htmleditor to the projects array list
projects.Add(htmleditor);

// add the projects array list to the List
projectsArrayList.Add(projects);

// add the projects array list to the dictionary list
myProjectsDicList.Add(domainName, projectsArrayList);
GRF
  • 171
  • 2
  • 10
  • Can I just say.. a single entry dictionary that holds a list of list of objects that will forever need casting, has got to be one of the most abstruse, unworkable data storage containers you could hope to implement. Are you seeking to enter a "making code hard to understand and maintain" competition? :) why is this not just a `Dictionary` so the htmlEditor can be simply retrieved by its project name, without a cast? Perhaps tell us more about what you're trying to achieve so we can recommend something easier.. – Caius Jard Apr 04 '20 at 04:54
  • I just added code above that works which is what I'm trying to achieve, but looking to try and use a Select statement to not have to use for loops. – GRF Apr 04 '20 at 16:43
  • Perhaps I wasn't clear. If you just use a dictionary then you don't even need a Select statement. Look: `Dim myProjectsDic = New Dictionary(Of String, HtmlEditor)` then `myProjectsDic("Project Name") = htmlEditor`. Later/anywhere you can then `Dim key = "Project Name"`, then `Dim htmlEdtr = myProjectsDic(key)` - right now the code is like a Heath Robinson cartoon. If you've reached the point where you think you need a Dictionary of Lists of ArrayLists, what you actually need is a custom class and a fresh approach – Caius Jard Apr 04 '20 at 18:11

0 Answers0