0

I am a fairly new C# programmer and am getting stuck on trying to convert FQL results into a custom class...for example, I am doing the following, but it seems like a lot of steps...I was just returning a datatable, but wanted the result to be strongly typed class collection. I'd appreciate any insights. I'm open to other ways of achieving similar results as well.

Thanks, Chad

public class FacebookFriends
{
    public string FriendID { get; set; }
    public string FriendName { get; set; }
    public string PicURLSquare { get; set; }
    public string ProfileLink { get; set; }

    //Gets your FB friends that are NOT currently using this application so you can invite them
    public IEnumerable<FacebookFriends> GetFriendsNotUsingApp()
    {
        string strQuery = "SELECT uid, name, pic_square, link FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me()) AND NOT is_app_user";

        FacebookSDKInterface objFQL = new FacebookSDKInterface();
        dynamic objFNU = objFQL.FBFQL(strQuery);

        //Construct the new, formated, merged datatable to store the results the way we want them   
        DataTable dtFriendsNotUsingApp = new DataTable();    
        dtFriendsNotUsingApp.Columns.Add("FriendID");
        dtFriendsNotUsingApp.Columns.Add("FriendName");
        dtFriendsNotUsingApp.Columns.Add("PicURLSquare");
        dtFriendsNotUsingApp.Columns.Add("Link");

        if (objFQL != null)
        {
            foreach (dynamic row in objFNU.data)
            {
                //Add New DataRow to new DataTable
                DataRow drRow = dtFriendsNotUsingApp.NewRow();

                //Get various values from original JSON Friend List returned
                drRow["FriendID"] = row.uid;
                drRow["FriendName"] = row.name;
                drRow["PicURLSquare"] = row.pic_square;
                drRow["Link"] = row.link;

                //Add New Row to New Resulting Data Table
                dtFriendsNotUsingApp.Rows.Add(drRow);
            }

            dtFriendsNotUsingApp.DefaultView.Sort = "FriendName";
        }

        IEnumerable<FacebookFriends> objFriendsListCollection = null;

        var toLinq = from list in dtFriendsNotUsingApp.AsEnumerable()
                     select new FacebookFriends
                     {
                         FriendID = list["FriendID"].ToString(),
                         FriendName = list["FriendName"].ToString(),
                         PicURLSquare = list["PicURLSquare"].ToString(),
                         ProfileLink = list["ProfileLink"].ToString()
                     };

        objFriendsListCollection = toLinq.OrderByDescending(p => p.FriendName);

        return objFriendsListCollection;

    } //Get FB Friends not already using this app

3 Answers 3

1

I belive this may help.

1st: I've never used the Facebook API, so I'm just using your code as an example.

2nd: As the method is inside the class, I've changed it to static. This way, you can use it by simply calling FacebookFriends.GetFriendsNotUsingApp(), instead of new FacebookFriends().GetFriendsNotUsingApp().

3rd The code:

    public class FacebookFriends
    {
        public string FriendID { get; set; }
        public string FriendName { get; set; }
        public string PicURLSquare { get; set; }
        public string ProfileLink { get; set; }

        //Gets your FB friends that are NOT currently using this application so you can invite them
        public static IEnumerable<FacebookFriends> GetFriendsNotUsingApp()
        {
            string strQuery = "SELECT uid, name, pic_square, link FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me()) AND NOT is_app_user";

            FacebookSDKInterface objFQL = new FacebookSDKInterface();
            dynamic objFNU = objFQL.FBFQL(strQuery);

            List<FacebookFriends> friendsToReturn = new List<FacebookFriends>();

            if (objFQL != null)
            {
                foreach (dynamic row in objFNU.data)
                {
                    friendsToReturn.Add(new FacebookFriends()
                        {
                            FriendID = row.uid,
                            FriendName = row.name,
                            PicURLSquare = row.pic_square,
                            ProfileLink = row.link
                        }
                    );
                }
            }

            return friendsToReturn;
        } //Get FB Friends not already using this app
    }

Hope this helps.

Regards

1
  • This worked perfectly. Thanks Andre...and for the additional pointers as well!
    – crichavin
    Jul 24, 2012 at 16:21
1

I have no experience with Facebook API or FQL as well, but by looking at your code objFNU.data appears to implement IEnumerable, hence you can use LINQ extension methods directly with it:

public class FacebookFriends
{
    public string FriendID { get; set; }
    public string FriendName { get; set; }
    public string PicURLSquare { get; set; }
    public string ProfileLink { get; set; }

    //Gets your FB friends that are NOT currently using this application so you can invite them
    public static IEnumerable<FacebookFriends> GetFriendsNotUsingApp()
    {
        string strQuery = "SELECT uid, name, pic_square, link FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me()) AND NOT is_app_user";

        FacebookSDKInterface objFQL = new FacebookSDKInterface();
        dynamic objFNU = objFQL.FBFQL(strQuery);

        if (objFQL != null) // shouldn't you check objFNU for being null here instead?
        {
            IEnumerable<dynamic> objFNUdata = (IEnumerable<dynamic>)objFNU.data; // explicit cast might not be necessary
            return objFNUdata.Select(row => new FacebookFriends()
                {
                    FriendID = row.uid,
                    FriendName = row.name,
                    PicURLSquare = row.pic_square,
                    ProfileLink = row.link
                }).OrderByDescending(p => p.FriendName);
        }
        else
        {
            return new List<FacebookFriends>();
        }
    } //Get FB Friends not already using this app
}
6
  • Thank you Damir for your response. I like this as it is clean and avoids for each loops which I'd think would have better performance. However, this gives the error: "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type" at both lamda lines (p => and row =>). Any thoughts?
    – crichavin
    Jul 24, 2012 at 16:02
  • @ChadRichardson Yes, it slipped my mind that you can't use lambda expressions with dynamically typed variables. It should be enough to cast your dynamic to an IEnumerable<dynamic>. I already edited the answer.
    – Damir Arh
    Jul 24, 2012 at 18:55
  • Thanks Damir...I'd really like to go this route, but that doesn't work either. It gives a "IEnumerable<dynamic> does not contain a definition for data" with regards to the "objFNU.data.Select" statement. Know anyway around that?
    – crichavin
    Jul 25, 2012 at 2:22
  • @ChadRichardson Sorry about that, objFNU.data is an IEnumerable, not objFNU. I edited the code above again. Any better now?
    – Damir Arh
    Jul 25, 2012 at 5:03
  • Hey Damir, well, the code compiles, but at runtime, I get an error: {"Cannot perform runtime binding on a null reference"}. Not sure what that means in this scope. When I view the value of objFNUdata, everything looks correct...there are no null values. So I can't figure out why it is stating this error. Any ideas? Thanks!
    – crichavin
    Jul 27, 2012 at 17:20
0

In the end, this worked best for me. Thanks to both, and especially @DarmirArh for all his help in getting this to work.

try
        {
            FacebookSDKInterface objFQL = new FacebookSDKInterface();
            dynamic objFNU = objFQL.FBFQL(strQuery);

            if (objFNU != null) // shouldn't you check objFNU for being null here instead?
            {
               IEnumerable<dynamic> objFNUdata = (IEnumerable<dynamic>)objFNU.data; // explicit cast might not be necessary
               IEnumerable<FacebookFriends> objMyFriends =
               from row in objFNUdata
               select new FacebookFriends()
               {
                   FriendID = row.uid,
                   FriendName = row.name,
                   PicURLSquare = row.pic_square,
                   ProfileLink = row.profile_url
               };

               objMyFriends = objMyFriends.OrderBy(p => p.FriendName);
               return objMyFriends;
            }
            else
            {
                return new List<FacebookFriends>();
            }
        }
        catch (Exception ex)
        {
            return new List<FacebookFriends>();
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.