将FQL结果转换为自定义类?

我是一个相当新的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 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 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 

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

第一 :我从未使用过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 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 friendsToReturn = new List(); 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 } 

希望这可以帮助。

问候

我也没有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 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 objFNUdata = (IEnumerable)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(); } } //Get FB Friends not already using this app } 

最后,这对我来说效果最好。 感谢两者,特别是@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 objFNUdata = (IEnumerable)objFNU.data; // explicit cast might not be necessary IEnumerable 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(); } } catch (Exception ex) { return new List(); }