【问题标题】:Conditional Linq to XML select (nested select statements)条件 Linq to XML 选择(嵌套选择语句)
【发布时间】:2011-03-29 09:47:41
【问题描述】:

我正在尝试查看我在 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 语句:

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

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

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

非常感谢您对此问题的任何帮助。

希望你能帮帮我!

格子

【问题讨论】:

    标签: xml linq facebook select facebook-fql


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的快速回复!我还没有完全找到它,但我想我现在已经很接近了。
    • 我已经编辑了我的问题。如果你介意再看一遍?非常感谢您的帮助!
    • 根据您的编辑更新了我的代码示例中的 where 语句。
    • 非常感谢!您的答案只需稍作修正即可。
    • 修正了什么?如果你把它放在评论中,我会为你更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2021-04-07
    相关资源
    最近更新 更多