1

How to add an element movie, to an existing category element? Currently im just able, to create new list entries on both fields.

class MovieSolver
    {
        public string Category { get; set; }
        public List<string> Movie { get; set; }
    }
class CreateMovieList
    {
        readonly static List<MovieSolver> playerTargetList = new List<MovieSolver>();

        public static void MovieCBO()
        {
            playerTargetList.Add(new MovieSolver { Category = "Action", Movie = new List<string> { "Taken", "The Factory", "Wind River" } });
            playerTargetList.Add(new MovieSolver { Category = "Comedy", Movie = new List<string> { "Gold", "Hangover", "We are the Millers" } });
            playerTargetList.Add(new MovieSolver { Category = "Thriller", Movie = new List<string> { "Jack Reacher", "Real Steel", "Iron Man I" } });
        }
    }

Beside is there a smarter way to create the class MovieSolver?

4
  • I assume your title is meant to be List<string>. Where does List<List<int>> come from. And what do you mean by 'smarter'? Commented May 15, 2020 at 16:32
  • Just not sure if i have done it the right way, as i have to to loop twice to access the List. Commented May 15, 2020 at 16:39
  • public Dictionary<string,List<string>> playerTargetList; Commented May 15, 2020 at 16:40
  • Three steps 1) Create top level elements : List<List<int>> numbers = new List<List<int>>; 2) Create one dimensions list : List<int> number = new List<int>() { 1,2,3,4,5}; 3) add one dimension list to two dimensional list numbers.Add(number); Commented May 15, 2020 at 16:42

3 Answers 3

1

First of all, i cant find a int list here at all.

If you want to add a new string object to the List in a MoveiSolver object you first need to find the Moviesolver object were you want to add the new string element.

You can do this with a for loop, foreach loop or with LINQ.

foreach is often the most easy to understand for new coders.

Foreach item in playerTargetList
{
 if(item.category == "Action")
 {
  item.Movie.add("lorum ipsum");
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the nice and proper explanation, its working perfectly :)
0

You could add a method addMovie to class CreateMovieList that verifies if the MovieSolver doesn't exist yet and creates it if necessary

public static void addMovie(String category, String movie) 
{
    var movieSolver = playerTargetList.Find(e => e.Category == category);
    if(movieSolver != null) 
    {
        var existingMovie = movieSolver.Movie.Find( e => e == movie);
        if (existingMovie == null)
            movieSolver.Movie.Add(movie);
    }
    else
        playerTargetList.Add(new MovieSolver  { Category = category, Movie = new List<string> { movie } }); 
}

1 Comment

Exactly this i have done based on the input from @saratcsss. addMovie by using the Dictionary solution. Thanks for support
0

Why don't you use a default class like Dictionary<string, List<string>> here?

//Add library reference
using System.Collections.Generic;

public void MyMethod()
{
    //Initialize a Dictionary
    Dictionary<string, List<string>> MovieCBO = new Dictionary<string, List<string>>();
    //Check if its an existing category
    //If its an existing category, add the movie to existing list
    if (MovieCBO.ContainsKey("some_category"))
    {
        MovieCBO["some_category"].Add("new_movie_name");
    }
    //If its a new category, add category along the movie
    else
    {
        MovieCBO.Add("some_category", new List<string>() { "new_movie_name" });
    }
}

All operations will be done in constant time. It would much faster than iterating through all the items in a list and checking the category.

4 Comments

Im currently trying to implement your solution, but i get no output, no error. I think is related to if (MovieCBO.ContainsKey("some_category")) . Should the if statement not be linked to playerTargetList This is actually what im currently trying to make the code work... I can accept just one answer. As soon your code above works im going to give you that tick.
What's the name of the dictionary you declared? if (<your_dictionary>.ContainsKey("key_value")) should be used.
i used your declaration Dictionary<string, List<string>> MovieCBO = new Dictionary<string, List<string>>();
As i understood the logic, is if a category is existing it will add just the movie, else category and movie... the code its complete on my initial post, there is just a button to run MovieCBO() and MyMethod()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.