有谁知道我为什么会收到此 asp.net 错误? CS1061 linqtotwitter

Does anyone know why I'm getting this asp.net error? CS1061 linqtotwitter

我尝试使用 linqtotwitter 搜索推文并使用此模型在 table 中显示它们:

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' does not contain a definition for 'ToListAsync' and no extension method 'ToListAsync' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)"

ToListAsync是一个带有参数cancellationToken的扩展方法,参见https://msdn.microsoft.com/de-de/library/dn220261.aspx。所以你需要给参数传递一个值cancellationToken来摆脱编译错误。

searchResponse.StatusesList<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();