Pregunta

I am writing a spec flow test with three columns... example:

|Field  | Key       | Value |
|A      | aa        |   ""  |
|       | bb        |   ""  |
|       | cc        |   33  |
|       | dd        |   1   |
|B      | aa        |   5   |
|       | bb        |   4   |
|       | cc        |   2   |
|       | dd        |   ""  |

I added this method to read data from the table:

      var elements = new Dictionary<string, Dictionary<string,string>>().ToList();
        var data = table.CreateSet<SpecFlowData>();
        elements.AddRange(collection: from item in data
                              let field =item.Field
                              let key = item.Key
                              let value = item.Value
                               select new Dictionary<string, Dictionary<string,string>>()
                                  {
                                      Keys = field,
                                      Values = new Dictionary<string,string>()
                                          {
                                              Keys= key,
                                              Values = value

                                          } 

                                  }

                              );

I am trying to assign a key value pair for each field in the first column.But I get error "Property without setter cannot be assigned to" while building the code. Any idea is appreciated. Thank you.

¿Fue útil?

Solución

You can't assign Keys and Values to Dictionary<> directly. But you can project data to proper Dictionary of Dictionaries using LINQ:

var result = data.GroupBy(i => i.Field, (k, v) => new
                          {
                             Key = k,
                             Values = v.ToDictionary(x => x.Key, x => x.Value)
                          })
                 .ToDictionary(x => x.Key, x => x.Values);

Otros consejos

You can't specifically set the Dictionary<,>.Keys or Dictionary<,>.Values properties. They are only get properties. You need to loop through your values and use the Add(Key, Value) or use the LINQ ToDictionary<TSource, TKey> method.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top