条件 Linq to XML 选择(嵌套选择语句)
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您指的是用户的
where
语句,该语句在您的示例中不存在。顶部查询(select new fbUser
)对所有用户执行选择。如果要过滤用户列表,则需要在from
和select
之间插入where
语句,如下所示:将数据查询转换为 LinQ 时存在的误解是,生成的 LinQ 查询应该是单个语句,但事实并非如此。在您的情况下,只有使用
Take(5)
语句时才会执行查询。因此,如果您在构建where
语句时遇到困难,可以将其推迟到创建select
部分之后: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 awhere
statement between thefrom
andselect
, like this: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 yourwhere
statement, you can postpone it until after you've created theselect
part: