C# ToDictionary lambda select index and element?

前端 未结 3 1039
野的像风
野的像风 2021-01-01 12:57

I have a string like string strn = \"abcdefghjiklmnopqrstuvwxyz\" and want a dictionary like:

Dictionary(){
    {\'a\',0},
    {         


        
3条回答
  •  庸人自扰
    2021-01-01 13:34

    You could try something like this:

        string strn = "abcdefghjiklmnopqrstuvwxyz";
    
    Dictionary lookup = strn.ToCharArray()
        .Select( ( c, i ) => new KeyValuePair( c, i ) )
            .ToDictionary( e => e.Key, e => e.Value );
    

提交回复
热议问题