2017-11-27 2 views
0

C# specflow 테이블에서 사전을 만들고 싶습니다. 레이블의 값으로이 값이 필요하며 모든 기능 파일에 대한 값이 변경됩니다. 현재 기능 파일에는 2 개의 열이 있습니다.specflow 테이블을 todictionary로 변환

And I submit with following 
| Label   | Value    | 
| Namelabel  | texttoenter123  | 
| Email   |[email protected]  | 

해당 단계. linq를 사용했습니다.

[When(@"I submit with following ")] 

    public void WhenISubmitWithFollowing(Table table) 
     { 
     //Should be something like this , which convert the table todictionary object 
      var x = table.Rows.ToDictionary(k=>k.Keys,k=>k.Values); 
     }`` 

현재이 문제는 null입니다. 도움을 받으십시오. 당신은 다음과 같은 주요

  DataTable dt = new DataTable(); 
      dt.Columns.Add("Label", typeof(string)); 
      dt.Columns.Add("Value", typeof(string)); 


      dt.Rows.Add(new object[] { "Namelabel", "texttoenter123" }); 
      dt.Rows.Add(new object[] { "Email", "[email protected]" }); 

      Dictionary<string, string> dict = dt.AsEnumerable() 
       .GroupBy(x => x.Field<string>("Label"), y => y.Field<string>("Value")) 
       .ToDictionary(x => x.Key, y => y.FirstOrDefault()); 

사용 당 하나 개의 값을 가질 경우

답변

0

당 하나 개 이상의 값 :

var x = table.Rows.ToDictionary(r => r[0], r => r[1]); 

그렇지 않은 경우 :

var x = table.Rows.ToDictionary(r => r["Label"], r => r["Value"]); 
+0

이것은 내가 찾던 것이다. 그것은 효과가 있었다. 감사 – Shanti

0

보십시오 다음과 같은 경우에는 테이블 열 머리글에 유연하게하려면 키

  Dictionary<string, List<string>> dict = dt.AsEnumerable() 
       .GroupBy(x => x.Field<string>("Label"), y => y.Field<string>("Value")) 
       .ToDictionary(x => x.Key, y => y.ToList()); 
+0

필자는 테이블을 일반 사전으로 변환하여 피쳐 파일에서 전달 된 모든 값에 대해 사전을 다시 사용할 수 있습니다. – Shanti