我正在尝试制作一个可以管理我的朋友的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
如您所见,我仍然在这里缺少一些代码。我不知道我是否仍然可以从这里修改查询。我希望你能告诉我我该怎么做。否则,请指出正确的方向以使之成为可能。
谢谢!
最佳答案
您的代码很混乱,无法编译。但是,当您清理它并更正错误时,最后您会看到friends
变量的类型为IEnumerable<User>
。之后,您可以像这样简单地继续在filterFriendsByName方法中进行过滤。
public void filterFriendsByName(string name)
{
return friends.Where(x=> x.name == name);
}
friends
不会更改,但是上面的方法将返回过滤的“按名称显示的好友”。城市也一样