0

我是一个相当新的 C# 程序员,并且一直在尝试将 FQL 结果转换为自定义类...例如,我正在执行以下操作,但似乎有很多步骤...我只是返回一个数据表,但希望结果是强类型的类集合。我会很感激任何见解。我也对实现类似结果的其他方法持开放态度。

谢谢,乍得

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
4

3 回答 3

1

我相信这可能会有所帮助。

第一:我从未使用过 Facebook API,所以我只是以您的代码为例。

第二:由于该方法在类中,我已将其更改为static. 这样,您可以通过简单地调用来使用它FacebookFriends.GetFriendsNotUsingApp(),而不是new FacebookFriends().GetFriendsNotUsingApp().

3号代码:

    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
    }

希望这可以帮助。

问候

于 2012-07-24T04:46:23.473 回答
1

我也没有使用 Facebook API 或 FQL 的经验,但是通过查看您的代码objFNU.data似乎实现了IEnumerable,因此您可以直接使用 LINQ 扩展方法:

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
}
于 2012-07-24T05:03:56.550 回答
0

最后,这对我来说效果最好。感谢两者,尤其是 @DarmirArh 为使这项工作发挥作用所提供的所有帮助。

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>();
        }
于 2012-07-27T18:07:04.153 回答