0

我正在尝试从我的 xml 文档中提取最后 10 个元素,我正在使用此代码来解析它:

slideView.ItemsSource = 
    from channel in xmlItems.Descendants("album") 
    let id = channel.Element("catid")
    let tit = channel.Element("name")
    let des = channel.Element("picture")
    orderby (int) id descending 
    select new onair
    {
        title = tit == null ? null : tit.Value,
        photo = des == null ? null : des.Value,
    };

请帮忙:)谢谢

4

1 回答 1

0

此代码将返回按以下顺序排序的最后 10 个元素catid

slideView.ItemsSource = 
    xmlItems.Descendants("album") 
            .OrderByDescending(channel => (int)channel.Element("catid"))
            .Select(channel => new onair
            {
                title = (string)channel.Element("name"),
                photo = (string)channel.Element("picture")
            }).Take(10);

与查询语法相同(好吧,几乎是查询语法 - Take 运算符没有关键字):

slideView.ItemsSource = 
    (from channel in xmlItems.Descendants("album")     
     orderby (int)channel.Element("catid") descending 
     select new onair
     {
         title = (string)channel.Element("name"),
         photo = (string)channel.Element("picture")
     }).Take(10);
于 2012-12-25T09:09:47.303 回答