web api'object'不包含'ToList'

时间:2018-04-06 14:34:09

标签: asp.net rest asp.net-web-api2

我真的看不出我在这里错过了什么 这是我在项目开发的另一方的请求下从GET重构到POST的代码。

        using VirtualAssistant.Models;
        using VirtualAssistant.DataModel;
        using System;
        using System.Collections.Generic;
        using System.Configuration;
        using System.Linq;
        using System.Net;
        using System.Net.Http;
        using System.Web.Http;
        using System.Data;
        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
        using System.Web.Http.Description;
        using System.Collections;

    namespace LloydsVirtualAssistant.Controllers
    {
        public class FetchController : ApiController
        {

            public class PersonList

            {
                public int ClientID { get; set; }
                public int PersonId { get; set; }
                public string FullName { get; set; }
                public string EmailAddress { get; set; }
                public string BusinessNumber { get; set; }
                public string MobileNumber { get; set; }
                public int LocationId { get; set; }
            public int BuidlingId { get; set; }
            public string LocationAddress { get; set; }
            public string PostCode { get; set; }
            public string BranchSortCode { get; set; }
            public string AreaName { get; set; }
        }




        // GET: ChatAi/Person/5
        [Route("FetchPerson")]

        public HttpResponseMessage POST([FromBody] PersonList PersonId)
        {
            List<PersonList> results = new List<PersonList>();
            var result = new FetchDataContext().usp_FetchPerson_GetById(PersonId).ToList();

            foreach (var item in result.ToList())
            {
                results.Add(new PersonList
                {
                    ClientID = (int)item.ClientID,
                    PersonId = (int)item.PersonId,
                    FullName = item.FullName,
                    EmailAddress = item.EmailAddress,
                    BusinessNumber = item.BusinessPhone,
                    MobileNumber = item.MobileNumber,
                    LocationId = (int)item.LocationId,
                    BuidlingId = (int)item.BuildingId,
                    LocationAddress = item.LocationAddress,
                    PostCode = item.PostCode,
                    BranchSortCode = item.BranchSortCode,
                    AreaName = item.AreaName
                });
            }
            {
                return results;
            }
        }
    }
}

错误读取

  

严重性
  代码描述项目文件行抑制状态   错误CS1061'对象'不包含'ToList'的定义,并且没有&gt;扩展方法'ToList'接受类型'object'的第一个参数可以被&gt;找到(你是否缺少using指令或汇编引用?) &GT; VirtualAssistant .....

这最初是作为一个GET工作,但团队想要一个POST。我的重构尝试失败了。我当然会继续寻找你是否有任何专业人士可以发现我的错误?这是我的第一个Web API - 好吧,如果你不计算正在运行的GET。我的专业领域是SQL,只是学习ASP.NET(并喜欢它)

非常感谢你提前看看。

1 个答案:

答案 0 :(得分:1)

您正在此处检索单个元素:

var result = new FetchDataContext().usp_FetchPerson_GetById(PersonId).ToList();

您只能在IEnumerable或集合,数组等上使用ToList() ...

请尝试以下方法:

var person = new FetchDataContext().usp_FetchPerson_GetById(PersonId);
if (person != null) {
    results.Add(person);
}