Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
public List<Movie> mType(List<string> fType)
       {
           List<Movie> fMovie = new List<Movie>();
           foreach(Movie movie in ListOfMovies)
           {
               if (fType = movie.Type)//this is where the error is
               {
                   fMovie.Add(movie);
               }//end of if
           }//end of foreach
           return fMovie;
       }


Cannot implicitly convert type 'string' to 'System.Collections.Generic.List<string>'

This is the error i am getting, any help will be appreciated
Posted
Updated 11-Dec-14 5:51am
v2
Comments
Afzaal Ahmad Zeeshan 11-Dec-14 11:24am    
On which line do you get error! There are other errors in your code too.

You cannot use '=' in this manner in the if statement either.
Member 10954859 11-Dec-14 11:51am    
yes it was == but that only applies to boolean, just starting to learn this language
Afzaal Ahmad Zeeshan 11-Dec-14 13:10pm    
Then asking a question, of which even you don't have an answer to doesn't make any sense.
Marcin Kozub 11-Dec-14 12:04pm    
Can you tell us what is ListOfMovies?
Afzaal Ahmad Zeeshan 11-Dec-14 13:10pm    
This entire question seems gibberish to me, there are a lot of syntax problems and the variables are not even well formatted and provided.

1 solution

I assume you want to check that movie type is one of the strings in the list...
Use List<T>.Contains(T)[^] method...
C#
if (fType.Contains(movie.Type))
{
  // ...
}


[Edit: Matt T Heffron]
If the fType is not very short, then this can be optimized using a HashSet<string> :
C#
public List<Movie> mType(List<string> fType)
{
  List<Movie> fMovie = new List<Movie>();
  HashSet<string> typesMap = new HashSet<string>(fType);
  foreach(Movie movie in ListOfMovies)
  {
    if (typesMap.Contains(movie.Type))
    {
      fMovie.Add(movie);
    }//end of if
  }//end of foreach
  return fMovie;
}


And using Linq this would be:
C#
public List<Movie> mType(List<string> fType)
{
  HashSet<string> typesMap = new HashSet<string>(fType);
  return (from movie in ListOfMovies where typesMap.Contains(movie.Type) select movie).ToList();
}
 
Share this answer
 
v3
Comments
BillWoodruff 11-Dec-14 12:25pm    
+5
Kornfeld Eliyahu Peter 11-Dec-14 12:29pm    
Thank you...also for fixing typo :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900