Question

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>(????));
Was it helpful?

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!

OTHER TIPS

 //.Select(new KeyValuePair<int,bool>(????));
 .Select(movie => new KeyValuePair<int,bool>() 
              { Key = movie.Id, Value = movie.IsReleased} );
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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top