由linq修复组

时间:2014-04-29 02:10:40

标签: .net windows linq windows-phone-8

XDocument xmlthandongdatviet = XDocument.Load(@"Assets/XMLthandongdatviet.xml");
List<thandong> thandong = (from query2 in xmlthandongdatviet.Elements("truyen").Elements("thandongdatviet")
    group query2 by query2.Element("taptruyen").Value into g
    select new thandong
    {
        page = g.Element("tieudetrang").Value,//error is not has Element
        seri = g.Key,
        urltitle = g.Element("duongdantieude").Value //error is not has Element
    }
).ToList();

先生,Windows Phone 8.1 SDK没有“g.element”。它只有“g.elementAt”或“g.elements”

档案图片:https://www.facebook.com/photo.php?f...type=1&theater

帮我修复它...谢谢

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说,每个组可能包含多个元素,因此使.Element()方法只返回一个元素是没有意义的。您的最终目标尚不清楚,使用FirstOrDefault()就足够了:

.......
page = (string)g.Elements("tieudetrang").FirstOrDefault(),
seri = g.Key,
urltitle = (string)g.Elements("duongdantieude").FirstOrDefault()
.......

如上面的示例所示,将元素转换为字符串,而不是访问元素的Value属性,以避免在未找到元素的情况下出现空引用异常。