FQL.Multiquery - 作为参数传递时的查询语法

时间:2012-07-27 05:09:11

标签: facebook-c#-sdk fql.multiquery

寻找一个简单任务的一点帮助... facebook多查询的语法,您可以在查询中作为参数传递。

这是我的方法:

public dynamic FBFQL(string strQuery)
    {
        try
        {
            var fb = new FacebookClient(this.FacebookAccessToken);
            //dynamic objFQL = fb.Get("fql", new { q = strQuery });
            dynamic objFQL = fb.Get("fql", new { q = new { strQuery } });

            if (objFQL == null)
            {
                return null;
            }
            else
            {
                return objFQL;
            }
        }
        catch (FacebookApiException ex)
        {
            FacebookErrorHandler(ex);
            return null;
        }
    } //FB FQL

我的查询语法"

string strQuery = "friendsMovies = \"SELECT page_id, uid FROM page_fan WHERE type='MOVIE' AND uid IN (SELECT uid2 FROM friend WHERE uid1=me()) ORDER BY page_id\"," +
" movieDetails = \"SELECT page_id, name, pic, page_url, fan_count, genre, starring, release_date FROM page WHERE page_id IN (SELECT page_id FROM #friendsMovies) ORDER BY page_id\"";

但不幸的是,它给出了以下错误。我试过几种方法都无济于事。我确定它很简单...我现在无法看到它。

(OAuthException - #601) (#601) Parser error: unexpected 'friendsMovies' at position 0.

提前致谢 乍得

1 个答案:

答案 0 :(得分:0)

计算出来并使用命名查询:

Calling方法并处理结果:

public List<FacebookMovie> GetMoviesLikedByFriends()
    {
        string strQuery0 = "SELECT page_id, uid FROM page_fan WHERE type='MOVIE' AND uid IN (SELECT uid2 FROM friend WHERE uid1=me()) ORDER BY page_id";
        string strQuery1 = "SELECT page_id, name, pic, page_url, fan_count, genre, starring, release_date FROM page WHERE page_id IN (SELECT page_id FROM #friendsMovies) ORDER BY page_id";

        Dictionary<string, object> dicQuery = new Dictionary<string, object>();
        dicQuery.Add("friendsMovies", strQuery0);
        dicQuery.Add("movies", strQuery1);

        FacebookSDKInterface objFQL = new FacebookSDKInterface();
        dynamic objMoviesFriendsLike = objFQL.FBMFQL(dicQuery);

        //To access a direct value: resultsMQFQL.data[0].fql_result_set[0].page_id

        if (objMoviesFriendsLike != null) // shouldn't you check objFNU for being null here instead?
        {
            IEnumerable<dynamic> friendsMovies = (IEnumerable<dynamic>)objMoviesFriendsLike.data[0].fql_result_set; // explicit cast might not be necessary
            IEnumerable<dynamic> movieDetails = (IEnumerable<dynamic>)objMoviesFriendsLike.data[1].fql_result_set; // explicit cast might not be necessary

            IEnumerable<FacebookMovie> objMyFriendsMovies = 
                from Movie in movieDetails
                join FriendsMovies in friendsMovies on (string)Movie.page_id equals (string)FriendsMovies.page_id
                group FriendsMovies by new
                {
                    ID = Movie.movie_id,
                    Link = Movie.page_url,
                    MovieName = Movie.name,
                    TotalLikes = Movie.fan_count,
                    Genre = Movie.genre,
                    Starring = Movie.starring,
                    ReleaseDate = Movie.release_date,
                    PicURL = Movie.pic
                } into grp
                where grp.Count() >= 2 //at least 2 friends must have liked it to show up
                select new FacebookMovie()
                {
                    Source = "Facebook",
                    ID = (string)grp.Key.ID,
                    SourceURL = (string)grp.Key.Link,
                    Name = (string)grp.Key.MovieName,
                    Picture = (string)grp.Key.PicURL,
                    TotalLikes = (long)grp.Key.TotalLikes,
                    Genre = (string)grp.Key.Genre,
                    Starring = (string)grp.Key.Starring,
                    ReleaseDate = (string)grp.Key.ReleaseDate,
                    FriendLikes = (int)grp.Count()
                };

            objMyFriendsMovies = objMyFriendsMovies.OrderByDescending(p => p.FriendLikes);

            return objMyFriendsMovies.ToList();
        }
        else
        {
            return new List<FacebookMovie>();
        }
    }

执行查询的方法:

public dynamic FBMFQL(Dictionary<string, object> dicQuery)
    {
        try
        {
            var fb = new FacebookClient(this.FacebookAccessToken);
            dynamic objFQL = fb.Get("fql", new { q = dicQuery });

            if (objFQL == null)
            {
                return null;
            }
            else
            {
                return objFQL;
            }
        }
        catch (FacebookApiException ex)
        {
            FacebookErrorHandler(ex);
            return null;
        }
    } //FB FQL