5

我正在尝试使用 lambda 表达式创建一个 KeyValue 对集合。

这是我的课程,下面是我的 lambda 代码。我未能创建 KeyValuePair。

我想为喜剧电影获取 ID 的 KeyValuePair、IsReleased 的集合。我将那些 KeyValuePair 放在 HashSet 中以便快速搜索

 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>(????));
4

3 回答 3

10

您应该将 lambda 传递给该.Select方法,而不仅仅是表达式:

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

希望有帮助!

于 2013-01-24T15:00:16.020 回答
2
 //.Select(new KeyValuePair<int,bool>(????));
 .Select(movie => new KeyValuePair<int,bool>() 
              { Key = movie.Id, Value = movie.IsReleased} );
于 2013-01-24T15:00:34.043 回答
1
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);
于 2013-01-24T15:03:10.567 回答