how to do a dictionary reverse lookup

前端 未结 5 1529
悲哀的现实
悲哀的现实 2021-02-05 04:23

I have a dictionary of type and for a particular case, I need to do a reverse lookup. So for instance suppose I have this entry <\"S

5条回答
  •  粉色の甜心
    2021-02-05 05:14

    Use the Linq ToDictionary function:

    var reversed = d.ToDictionary(x => x.Value, x => x.Key);
    

    You can see below that it works, as tested in Linqpad:

    var d = new Dictionary();
    d.Add(1,"one");
    d.Add(2,"two");
    d.Dump(); //prints it out in linq-pad
    var reversed = d.ToDictionary(x => x.Value, x => x.Key);
    reversed.Dump(); //prints it out in linq-pad
    

提交回复
热议问题