条件Linq到XML选择(嵌套选择语句)

时间:2011-03-29 09:47:42

标签: xml linq facebook select facebook-fql

我正在尝试查看来自facebook的所有去过某所学校的朋友。我使用FQL获取一个xml文件,其中包含我的所有朋友的教育信息。但我不能使用FQL来选择那些去过同一所学校的人。因此,我正在尝试使用Linq to XML来仅选择我想要的用户。我尝试了一些方法,但那些方法对我不起作用。

这是我的linq查询:

XDocument doc = RemoveNamespace (XDocument.Load(url));

        string[] search = {"Katho", "katho", "kortrijk" , "vhti"};
       List<string> keys = new List<string>( search );



        var user = (from usr in doc.Descendants("user")

                                   select new fbUser
                                   {

                                       name = usr.Element("name").Value,

                                       email = usr.Element("email").Value,
                                       current_location = StringExtensions.checkNull(usr.Element("current_location").Value,""),
                                       hometown_location = usr.Element("hometown_location").Value,
                                       pic = StringExtensions.checkNull(usr.Element("pic_square").Value,"http://www.fox16.com/media/avatar/default.jpg"),
                                       education_history = (from edu in usr.Descendants("education_info")
                                                           //where usr.Element("education_history").HasElements
                                                           where keys.Contains(edu.Element("name").Value)
                                                           select new Education
                                                           {

                                                               name = StringExtensions.checkNull(edu.Element("name"),""),
                                                               year = StringExtensions.checkNull(edu.Element("year"),""),
                                                               school_type = StringExtensions.checkNull(edu.Element("school_type"),""),
                                                               concentrations = from conc in edu.Descendants("concentrations")
                                                                                where (edu.Element("concentrations").HasElements)
                                                                                select new Concentration
                                                                                {
                                                                                    name = StringExtensions.checkNull(conc.Element("concentration").Value, "")
                                                                                }
                                                           })

                                   }).Take(5)
                                   ;

编辑:样本xml可以在这里找到:sample XML

还有一个问题。学校名称不是fbUser对象的直接属性。同样在xml文件中,学校名称只能在这里找到

<user><education_history><education_info><name>

所以这是我的声明:

where usr.Element("education_history").Element("education_info").Element(name").value == "schoolname"

问题是xml中并不总是有一个education_history或education_info节点。

所以我还需要一种方法来解决这个问题。

非常感谢任何有关此问题的帮助。

我希望你能帮助我!

Grtz

1 个答案:

答案 0 :(得分:1)

您指的是有关用户的where声明,您的示例中没有该声明。顶部查询(select new fbUser)对所有用户执行选择。如果您要过滤用户列表,则需要在wherefrom之间插入select语句,如下所示:

    var user = (from usr in doc.Descendants("user")
            where usr.Element("education_history") != null
                && usr.Element("education_info").Element(name") != null
                && usr.Element("education_info").Element(name").value == "schoolname"

            select new fbUser
            {
                //...
            }

此外,将数据查询转换为LinQ时常见的误解是生成的LinQ查询应该是单个语句,而不必是。在您的情况下,在您使用Take(5)语句之前不会执行查询。因此,如果您在构建where语句时遇到问题,可以将其推迟到创建select部分之后:

var query = (from usr in doc.Descendants("user")
             select new fbUser
                   //...

var user = query.where(u => 
    u.Element("education_history") != null
        && u.Element("education_info").Element(name") != null
        && u.Element("education_info").Element(name").value == "schoolname"
).Take(5);
相关问题