Search code examples
c#lambdakeyvaluepair

KeyValuePair in Lambda expression


I am trying to create a KeyValue pair collection with lambda expression.

Here is my class and below that my lambda code. I failed to create the KeyValuePair.

I want to get a collection of KeyValuePair of Id, IsReleased for the comedy movies. I put those KeyValuePair in HashSet for quick search.

 public class Movie{
  public string Name{get;set;}
  public int Id{get;set;}
  public bool IsReleased{get;set;}
  //etc
 }

List<Movie> movieCollection=//getting from BL

var movieIdReleased= new 
HashSet<KeyValuePair<int,bool>>(movieCollection.Where(mov=> mov.Type== "comedy")
                                    .Select(new KeyValuePair<int,bool>(????));

Solution

  • You should pass lambda into that .Select method, not just expression:

    .Select(movie => new KeyValuePair<int,bool>(movie.Id, movie.IsReleased))
    

    hope that helps!