KeyValuePair in Lambda expression

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

Here is my class and below that is my lambda code. I was unable to create KeyValuePair.

I want to get a collection of KeyValuePair Id, IsReleased for  comedy films. I put these KeyValuePair in a HashSet for a 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>(????));
+5
source share
3 answers

You should pass lambda to this .Selectmethod, not just the expression:

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

hope this helps!

+8
source
 //.Select(new KeyValuePair<int,bool>(????));
 .Select(movie => new KeyValuePair<int,bool>() 
              { Key = movie.Id, Value = movie.IsReleased} );
+2
source
var comedyMovies = movieCollection
    .Where(mc => "comedy".Equals(mc.Type, StringComparison.OrdinalIgnoreCase))
    .Select(mc => new KeyValuePair<int, bool>(mc.Id, mc.IsReleased));
var distinctComedyMovies = new HashSet<KeyValuePair<int,bool>>(comedyMovies);
+1
source

All Articles