转换FQL结果自定义类? [英] Convert FQL Results to custom class?
问题描述
我是一个相当新的C#程序员,我被陷在尝试转换FQL成果转化为一个自定义类...例如,我做以下,但似乎很多步骤......我只是返回一个DataTable,但希望是强类型集合类的结果。我会很感激任何见解。我打开实现了类似的结果,以及其他方式。
谢谢,
乍得
公共类FacebookFriends
{
公共字符串FriendID {搞定;组; }
公共字符串FRIENDNAME {搞定;组; }
公共字符串PicURLSquare {搞定;组; }
公共字符串ProfileLink {搞定;组; }
//获取当前未使用此应用的FB的朋友,所以你可以邀请他们
公开的IEnumerable< FacebookFriends> GetFriendsNotUsingApp()
{
串strQuery =SELECT UID,名称,pic_square,链接,从用户其中uid IN(SELECT UID2从相知WHERE UID1 =我())AND NOT is_app_user
FacebookSDKInterface objFQL =新FacebookSDKInterface();
动态objFNU = objFQL.FBFQL(strQuery);
//构建新的,格式化,合并数据表来存储结果的方式,我们希望他们
的DataTable dtFriendsNotUsingApp =新的DataTable();
dtFriendsNotUsingApp.Columns.Add(FriendID);
dtFriendsNotUsingApp.Columns.Add(FRIENDNAME);
dtFriendsNotUsingApp.Columns.Add(PicURLSquare);
dtFriendsNotUsingApp.Columns.Add(链接);
如果(objFQL!= NULL)
{
的foreach(动态在objFNU.data行)
{
//添加新的DataRow到新的DataTable
的DataRow drRow = dtFriendsNotUsingApp.NewRow();
//由原来的JSON好友列表获取不同的值返回
drRow [FriendID] = row.uid;
drRow [FRIENDNAME] = row.name;
drRow [PicURLSquare] = row.pic_square;
drRow [链接] = row.link;
//添加新行新生成的数据表
dtFriendsNotUsingApp.Rows.Add(drRow);
}
dtFriendsNotUsingApp.DefaultView.Sort =FRIENDNAME;
}
IEnumerable的< FacebookFriends> objFriendsListCollection = NULL;
VAR toLinq =从dtFriendsNotUsingApp.AsEnumerable()
列表中选择新的FacebookFriends
{
FriendID =列表[FriendID]。的ToString(),
FRIENDNAME =列表[FRIENDNAME]。的ToString(),
PicURLSquare =列表[PicURLSquare]。的ToString(),
ProfileLink =列表[ProfileLink]。的ToString()
};
objFriendsListCollection = toLinq.OrderByDescending(P => p.FriendName);
返回objFriendsListCollection;
} //获取FB好友尚未使用这个程序,
我相信这可能有助于
1 :我从来没有使用Facebook的API,所以我:M只是用你的代码作为一个例子
2日:由于该方法是在类中,我已经把它改为静态
。通过这种方式,你可以通过简单地调用 FacebookFriends.GetFriendsNotUsingApp()
使用它,而不是新FacebookFriends()。GetFriendsNotUsingApp()$ C $ 。C>
3 代码:
公共类FacebookFriends
{
公共字符串FriendID {搞定;组; }
公共字符串FRIENDNAME {搞定;组; }
公共字符串PicURLSquare {搞定;组; }
公共字符串ProfileLink {搞定;组; }
//获取当前未使用此应用的FB的朋友,所以你可以邀请他们
公共静态的IEnumerable< FacebookFriends> GetFriendsNotUsingApp()
{
串strQuery =SELECT UID,名称,pic_square,链接,从用户其中uid IN(SELECT UID2从相知WHERE UID1 =我())AND NOT is_app_user
FacebookSDKInterface objFQL =新FacebookSDKInterface();
动态objFNU = objFQL.FBFQL(strQuery);
名单,LT; FacebookFriends> friendsToReturn =新的List< FacebookFriends>();
如果(objFQL!= NULL)
{
的foreach(动态在objFNU.data行)
{
friendsToReturn.Add(新FacebookFriends()
{
FriendID = row.uid,
FRIENDNAME = row.name,
PicURLSquare = row.pic_square,
ProfileLink = row.link
$} b $ b);
}
}
返回friendsToReturn;
} //获取FB好友尚未使用这个程序,
}
希望这有助于。
问候
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
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
这篇关于转换FQL结果自定义类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!