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

发布于 2024-10-27 12:31:16 字数 3296 浏览 1 评论 0原文

我正在尝试从 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 可以在这里找到:示例 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 节点。

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

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

我希望你能帮助我!

格茨

I'm trying to view all my friends from facebook who went to some school. I use FQL to get an xml file that contains all my friends with their education info. But i can't use FQL to select only those who went to the same school. Therefor, I'm trying to use Linq to XML to select only the users i want. I've tried some methods but those won't work for me.

Here is my linq query:

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

EDIT: sample xml can be found here:sample XML

There's one more problem. The school name is not an direct attribute from the fbUser object. Also in the xml file the schoolname can only be found here

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

So here is my where statement:

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

Problem is that there is not always an education_history or education_info node in the xml.

So i also need a way to work around this..

Any help on this problem is very appreciated.

I hope you can help me!

Grtz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-11-03 12:31:16

您指的是用户的 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);

You're referring to a where statement on users, which is not present in your sample. The top query (select new fbUser) performs a select on all users. If you want to filter your user list, you need to insert a where statement between the from and select, like this:

    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
            {
                //...
            }

Also, a common misconception made when stranslating data queries into LinQ is that the resulting LinQ query should be a single statement, which it doesn't have to be. In your case, the query isn't executed until you use the Take(5) statement. So if you have trouble constructing your where statement, you can postpone it until after you've created the select part:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文