2014-02-15 95 views
0

我想從JSON訪問元素並將其綁定到XAML。 我的JSON代碼:C#where select clause

{ 
    "id":1474976, 
    "date":"2014-02-12 20:00:00", 
    "dateiso":"2014-02-12T20:00:00+00:00", 
    "competition_id":831975, 
    "competition":"England Premier League", 
    "group":"", 
    "home_id":9879, 
    "home":"Fulham", 
    "homeshort":"Fulham", 
    "homepath":"fulham", 
    "away_id":8650, 
    "away":"Liverpool", 
    "awayshort":"Liverpool", 
    "awaypath":"liverpool", 
    "status":"Finished", 
    "halftime":[ 
     1, 
     1 
    ], 
    "fulltime":[ 
     2, 
     3 
    ], 
    "extratime":[ 
     0, 
     0 
    ], 
    "penalties":[ 
     0, 
     0 
    ], 
    "incidents":[ 
     { 
     "id":2762178, 
     "type":"Own Goal", 
     "goaltype":"Own goal", 
     "team_id":9879, 
     "team":"Fulham", 
     "teamshort":"Fulham", 
     "teampath":"fulham", 
     "player_id":30831, 
     "player":"Kolo Tour\u00e9", 
     "playershort":"K. Tour\u00e9", 
     "minute":8 
     }, 
     { 
     "id":2762179, 
     "type":"Yellow", 
     "goaltype":null, 
     "team_id":9879, 
     "team":"Fulham", 
     "teamshort":"Fulham", 
     "teampath":"fulham", 
     "player_id":26782, 
     "player":"William Kvist", 
     "playershort":"W. Kvist", 
     "minute":9 
     }, 
     { 
     "id":2762207, 
     "type":"Goal", 
     "goaltype":"Regular goal", 
     "team_id":8650, 
     "team":"Liverpool", 
     "teamshort":"Liverpool", 
     "teampath":"liverpool", 
     "player_id":51553, 
     "player":"Daniel Sturridge", 
     "playershort":"D. Sturridge", 
     "minute":41 
     }, 
     { 
     "id":2762238, 
     "type":"Yellow", 
     "goaltype":null, 
     "team_id":9879, 
     "team":"Fulham", 
     "teamshort":"Fulham", 
     "teampath":"fulham", 
     "player_id":31290, 
     "player":"Sascha Riether", 
     "playershort":"S. Riether", 
     "minute":50 
     }, 
     { 
     "id":2762248, 
     "type":"Yellow", 
     "goaltype":null, 
     "team_id":8650, 
     "team":"Liverpool", 
     "teamshort":"Liverpool", 
     "teampath":"liverpool", 
     "player_id":184536, 
     "player":"Philippe Coutinho", 
     "playershort":"P. Coutinho", 
     "minute":56 
     }, 
     { 
     "id":2762260, 
     "type":"Goal", 
     "goaltype":"Regular goal", 
     "team_id":9879, 
     "team":"Fulham", 
     "teamshort":"Fulham", 
     "teampath":"fulham", 
     "player_id":30352, 
     "player":"Kieran Richardson", 
     "playershort":"K. Richardson", 
     "minute":64 
     }, 
     { 
     "id":2762262, 
     "type":"Yellow", 
     "goaltype":null, 
     "team_id":9879, 
     "team":"Fulham", 
     "teamshort":"Fulham", 
     "teampath":"fulham", 
     "player_id":30352, 
     "player":"Kieran Richardson", 
     "playershort":"K. Richardson", 
     "minute":65 
     }, 
     { 
     "id":2762279, 
     "type":"Goal", 
     "goaltype":"Regular goal", 
     "team_id":8650, 
     "team":"Liverpool", 
     "teamshort":"Liverpool", 
     "teampath":"liverpool", 
     "player_id":184536, 
     "player":"Philippe Coutinho", 
     "playershort":"P. Coutinho", 
     "minute":72 
     }, 
     { 
     "id":2762296, 
     "type":"Yellow", 
     "goaltype":null, 
     "team_id":8650, 
     "team":"Liverpool", 
     "teamshort":"Liverpool", 
     "teampath":"liverpool", 
     "player_id":156008, 
     "player":"Jordan Henderson", 
     "playershort":"J. Henderson", 
     "minute":77 
     }, 
     { 
     "id":2762307, 
     "type":"Goal", 
     "goaltype":"Penalty", 
     "team_id":8650, 
     "team":"Liverpool", 
     "teamshort":"Liverpool", 
     "teampath":"liverpool", 
     "player_id":30618, 
     "player":"Steven Gerrard", 
     "playershort":"S. Gerrard", 
     "minute":90 
     }, 
     { 
     "id":2762311, 
     "type":"Yellow", 
     "goaltype":null, 
     "team_id":9879, 
     "team":"Fulham", 
     "teamshort":"Fulham", 
     "teampath":"fulham", 
     "player_id":30839, 
     "player":"Johnny Heitinga", 
     "playershort":"J. Heitinga", 
     "minute":90 
     }, 
     { 
     "id":2762312, 
     "type":"Yellow", 
     "goaltype":null, 
     "team_id":8650, 
     "team":"Liverpool", 
     "teamshort":"Liverpool", 
     "teampath":"liverpool", 
     "player_id":30618, 
     "player":"Steven Gerrard", 
     "playershort":"S. Gerrard", 
     "minute":90 
     } 
    ] 
} 

我的C#代碼:

GoalsList.ItemsSource = 
     results.Where(x=>x.id==football.Livestring).Select(y=>y.incidents) 
      .Select(z=>z.Select(m=>new 
       { 
        player=m.player.ToString(), 
        goalminute=m.minute.ToString(), 
       })).ToList(); 

XAML代碼:

<ListBox Name="GoalsList"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
     <TextBlock Text="{Binding player}" Foreground="White"> 
      <Run Text="{Binding goalminute}" Foreground="White"></Run> 
     </TextBlock> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 



public void getId(object sender, DownloadStringCompletedEventArgs e) 
{ 
    try 
    { 
     if (!string.IsNullOrEmpty(e.Result)) 
     { 
      GoalsList.ItemsSource = results.Where(x => x.id==football.Livestring) 
              .Select(y => y.incidents) 
              .Select(z => z.S‌​elect(m => new 
                { 
                 player = m.player, 
                 goalminute = m.minute, 
                })).ToList(); 
     } 
    } 

沒有得到球員和go​​alminute的值。怎麼了?我試圖訪問一些值,他們工作正常,但是當我嘗試訪問事件數組它不work.help

+0

發佈'結果'代碼,它是如何填充的? –

+0

此外,你可以確認你的查詢返回一些結果嗎? (通過檢查調試模式) –

+0

是的,GoalList.Itemsource查詢適用於其他某些where子句條件,但可能與此問題有一些樹問題。 – Nachiket

回答

1

我認爲你需要一個平面列表,但你會得到列表清單。嘗試使用SelectMany這樣的方法:

GoalsList.ItemsSource = results.Where(x => x.id == football.Livestring) 
             .Select(y => y.incidents) 
             .SelectMany(z => z.S‌​elect(m => new 
               { 
                player = m.player, 
                goalminute = m.minute, 
               })).ToList(); 
+0

它的工作謝謝:)是什麼問題? – Nachiket

+0

注意到這一點。 @nachiket - 閱讀[Select Vs SelectMany](http://stackoverflow.com/questions/958949/difference-between-select-and-selectmany)。 –

0

1)你可以調試(進入)你的代碼?

2)這將是有用的,如果你可以先分割你LINQ成幾個語句,例如:

var football = results.Where(x => x.id==football.Livestring); 
var incidents = football.Select(y => y.incidents); 
var playerGoalMinute = incidents.Select(z => z.S‌​elect(m => new 
               { 
                player = m.player, 
                goalminute = m.minute, 
               }); 
GoalsList.ItemsSource = playerGoalMinute.ToList(); 

,並檢查是否每個變量(或它的計數)是如預期。