ThdK
ThdK

Reputation: 10556

Reusable Linq to XML methods to filter my query results

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!

Upvotes: 1

Views: 395

Answers (1)

archil
archil

Reputation: 39501

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 type IEnumerable<User>. After that, you can continue filtering in your filterFriendsByName method, simply as this.

public void filterFriendsByName(string name) 
{
    return friends.Where(x=> x.name == name);
}

friends will not be changed, but method above will return filtered Friends By Name. Same for city

Upvotes: 4

Related Questions