繁体   English   中英

C#的select子句

[英]C# where select clause

我正在尝试从JSON访问元素并将其绑定到XAML。 我的JSON代码:

{
   "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();
        }
    }

没有获得球员和进球的价值。 怎么了? 我尝试访问一些值,它们工作正常,但是当我尝试访问事件数组时,它不起作用.help

我认为您需要一个固定列表,但您会获得列表列表。 尝试使用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();

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();

并检查每个变量(或其计数)是否符合预期。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM