可重用 Linq to XML 方法来过滤我的查询结果
我正在尝试制作一个可以管理我的朋友的 Facebook 应用程序。现在我正在进行某种高级搜索。我使用 FQL 和 LINQ to XML 来适应我的搜索。
但我希望我的搜索方法可以重用。所以我可以组合多个过滤器。
这是我的想法:
private var friends;
public setFriends(XDocument doc)
{
friends = (from usr in doc.Descendants("user")
select new User
{
name = usr.Element("name").Value,
email = usr.Element("email").Value,
pic = usr.Element("pic_square").Value,
city = usr.Element("city").Value,
});
return friends;
}
public void filterFriendsByName()
{
friends = // some code to filter my previous results by name
}
public void filterFriendsByCity()
{
friends = // some code to filter friends by city
}
//more filters
如您所见,我仍然缺少一些代码。我不知道我是否仍然可以从这里修改我的查询。我希望你能告诉我如何做到这一点。或者为我指明正确的方向,使之成为可能。
谢谢你!
I'm trying to make an facebook application where i can manage my friends. Now i'm making some kind of advanced search. I use FQL and LINQ to XML to fit my search.
But i want my search methods to be reusable. So i can combine multiple filters.
Here's my idea:
private var friends;
public setFriends(XDocument doc)
{
friends = (from usr in doc.Descendants("user")
select new User
{
name = usr.Element("name").Value,
email = usr.Element("email").Value,
pic = usr.Element("pic_square").Value,
city = usr.Element("city").Value,
});
return friends;
}
public void filterFriendsByName()
{
friends = // some code to filter my previous results by name
}
public void filterFriendsByCity()
{
friends = // some code to filter friends by city
}
//more filters
As you can see, i'm still missing some code here. I don't know if i still can modify my query from here. I hope you can tell me how i could do this. Or else point me in the right direction to make this possible.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码非常混乱,还无法编译。但是,当您清理它并更正错误时,最后您会看到
friends
变量将获得类型IEnumerable
。之后,您可以继续在您的filterFriendsByName方法中进行过滤,就像这样。friends
不会更改,但上面的方法将返回按姓名过滤的好友。城市也一样Your code is pretty messed up and won't compile yet. But when you clean it up and correct errors, at last you'll see that
friends
variable will get typeIEnumerable<User>
. After that, you can continue filtering in your filterFriendsByName method, simply as this.friends
will not be changed, but method above will return filtered Friends By Name. Same for city