使用linqtotwitter我试图搜索推文并使用此模型在表格中显示它们:
using System;
using System.ComponentModel;
namespace test.Models
{
public class TweetSearchModel
{
[DisplayName("Tweet ID")]
public ulong ID { get; set; }
[DisplayName("Screen Name")]
public string ScreenName { get; set; }
[DisplayName("Tweet")]
public string Text { get; set; }
internal object ToListAsync()
{
throw new NotImplementedException();
}
}
}
此观点:
@model IEnumerable<test.Models.TweetSearchModel>
@{
ViewBag.Title = "Search";
}
<table>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ScreenName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Text)
</td>
</tr>
}
</table>
和这个控制器:
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using aLevel.Models;
using LinqToTwitter;
using System.Collections.Generic.I;
using System.Data.Entity;
namespace test.Controllers
{
public class QueriesController : Controller
{
// GET: Queries
public ActionResult Index()
{
return View();
}
[ActionName("Search")]
public async Task<ActionResult> SearchAsync()
{
var auth = new MvcAuthorizer
{
CredentialStore = new SessionStateCredentialStore()
};
var ctx = new TwitterContext(auth);
var searchResponse =
await
(from search in ctx.Search
where search.Type == SearchType.Search &&
search.Query == "\"LINQ to Twitter\""
select search)
.SingleOrDefaultAsync();
var tweets =
await
(from tweet in searchResponse.Statuses
select new TweetSearchModel
{
ID = tweet.ID,
ScreenName = tweet.ScreenName,
Text = tweet.Text
})
.ToListAsync();
return View(tweets);
}
}
}
然而“。ToListAsync”引发错误“CS1061'IEnumerable'不包含'ToListAsync'的定义,并且没有扩展方法'ToListAsync'接受类型'IEnumerable'的第一个参数可以找到(是你吗?)缺少using指令或程序集引用?)“
答案 0 :(得分:0)
ToListAsync
是带参数cancellationToken
的扩展方法,请参阅https://msdn.microsoft.com/de-de/library/dn220261.aspx。因此,您需要将值传递给参数cancellationToken
以消除编译器错误。
答案 1 :(得分:0)
searchResponse.Statuses
是List<Status>
,因此您正在执行LINQ to Objects查询。 LINQ to Objects(Enumerable)没有ToListAsync
,我不确定它应该有,因为查询不会导致进程外操作 - 它在内存中只要。您可以使用非异步运算符,如下所示:
var tweets =
(from tweet in searchResponse.Statuses
select new TweetSearchModel
{
ID = tweet.ID,
ScreenName = tweet.ScreenName,
Text = tweet.Text
})
.ToList();