想要将谓词传递给方法以获取Dictionary结果

时间:2013-04-26 02:25:24

标签: c# dictionary

基本上我想将一个谓词传递给一个方法,该方法产生一个字典结果,其中Entity与特定的Id相关联。但这似乎不起作用..

public Dictionary<int, Room> GetRooms(Func<KeyValuePair<int, Room>, bool> predicate)
{
      return _rooms.Where(predicate).ToDictionary<Room, int>(x => x.Key);
}

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

return语句应该返回Dictionary<int,Room>,而不是Dictionary<Room,int>。您还需要选择该值,否则您将获得Dictionary<int,KeyValuePair<int,Room>>

return _rooms.Where(predicate).ToDictionary(x => x.Key, x => x.Value);