通过C#使用LINQ访问twitter
我正在尝试在VS中创建Winforms应用程序,以搜索特定关键字并检索应用程序中的tweet显示。 下面是我的代码,它试图从我的个人资料中获取100条tweet,并在表单的构造函数中调用新方法,然后将数据分配到表单上的主tweets列表框通过C#使用LINQ访问twitter,c#,twitter,linq-to-twitter,C#,Twitter,Linq To Twitter,我正在尝试在VS中创建Winforms应用程序,以搜索特定关键字并检索应用程序中的tweet显示。 下面是我的代码,它试图从我的个人资料中获取100条tweet,并在表单的构造函数中调用新方法,然后将数据分配到表单上的主tweets列表框 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using LinqToTwitter;
public partial class Form1 : Form
{
private SingleUserAuthorizer authorizer =
new SingleUserAuthorizer
{
CredentialStore = new
SingleUserInMemoryCredentialStore
{
ConsumerKey ="",
ConsumerSecret ="",
AccessToken ="",
AccessTokenSecret =""
}
};
private List<Status> currentTweets;
public Form1()
{
InitializeComponent();
GetMostRecent100HomeTimeLine();
lstTweetList.Items.Clear();
currentTweets.ForEach(tweet =>
lstTweetList.Items.Add(tweet.Text));
}
private void GetMostRecent100HomeTimeLine()
{
var twitterContext = new TwitterContext(authorizer);
var tweets = from tweet in twitterContext.Status
where tweet.Type == StatusType.Home &&
tweet.Count == 100
select tweet;
currentTweets = tweets.ToList();
}
我猜是LinqToTwitter库试图枚举“tweets”集合,发现要么没有数据,要么无法检索数据。
我是这个主题的初学者,这是我的项目
希望有人能指导我解决这个问题LINQ到Twitter是异步的。因此,您应该在代码中使用
async
和wait
关键字。这也意味着将查询放在构造函数中不是一个好主意。在本例中,我将使用Form.Load
事件。假设您将Form1\u Load
处理程序连接到Form1
Load
事件,您可以这样重新编写代码:
public async void Form1_Load()
{
await GetMostRecent100HomeTimeLine();
lstTweetList.Items.Clear();
currentTweets.ForEach(tweet =>
lstTweetList.Items.Add(tweet.Text));
}
private async Task GetMostRecent100HomeTimeLine()
{
var twitterContext = new TwitterContext(authorizer);
var tweets = from tweet in twitterContext.Status
where tweet.Type == StatusType.Home &&
tweet.Count == 100
select tweet;
currentTweets = await tweets.ToListAsync();
}
请注意,Form1\u Load
方法是async
。它还返回void
,这是必需的,并且只能在顶级异步事件处理程序中使用。它等待GetMostRecent100HomeTimeLine
返回的任务
GetMostRecent100HomeTimeLine
方法也是async
,但返回Task
。异步调用链中初始async void
事件处理程序下面的所有方法都应返回Task
或Task
。这允许您等待
他们的任务,以便在被调用的方法完成之前调用方法不会返回
最后,请注意,代码等待从ToListAsync
返回的任务。虽然编译器允许您使用ToList
(没有wait),但它在运行时无法工作,因为LINQ-to-Twitter是异步的,并希望您等待它的操作符,如ToListAsync
、SingleOrDefaultAsync
或FirstOrDefaultAsync
您还可以使用try/catch
处理程序捕获抛出的任何异常。LINQ to Twitter将大多数异常包装在TwitterQueryException
中,该异常将包含来自Twitter API的任何错误消息和相关代码 请在问题中添加内部异常类型和文本。您的where
语句中的&&tweet.Count==100
子句看起来有误。这将仅在Count
为100时选择一条tweet。当然,情况并非总是如此。如果你把它拿出来会发生什么?还是会发生同样的问题
public async void Form1_Load()
{
await GetMostRecent100HomeTimeLine();
lstTweetList.Items.Clear();
currentTweets.ForEach(tweet =>
lstTweetList.Items.Add(tweet.Text));
}
private async Task GetMostRecent100HomeTimeLine()
{
var twitterContext = new TwitterContext(authorizer);
var tweets = from tweet in twitterContext.Status
where tweet.Type == StatusType.Home &&
tweet.Count == 100
select tweet;
currentTweets = await tweets.ToListAsync();
}