无法从IQueryable <object>获取数据

时间:2018-06-02 09:16:15

标签: asp.net asp.net-mvc angular

我在服务器中有这个来从数据库中获取数据:

[HttpPost]
[Route("api/getaddress")]
public IQueryable<PreAddress> GetAddress()
{
    var httpRequest = HttpContext.Current.Request;
    var id = httpRequest["personId"];
    int personId;
    if (!Int32.TryParse(id, out personId)) return null;
    using (var db = new ApplicationDbContext())
    {
        var preAddress = (from pa in db.PersonAndAddresses
            join a in db.Addresses on pa.AddressId equals a.AddressId
            join b in db.PersonBarangays on a.BarangayCode equals b.BarangayCode
            join m in db.PersonMunicipals on a.MunicipalCode equals m.MunicipalCode
            join p in db.PersonProvinces on a.ProvinceCode equals p.ProvinceCode
            join r in db.PersonRegions on a.RegionCode equals r.RegionCode
            where pa.PersonId == personId
            select new PreAddress()
            {
                BarangayCode = b.BarangayName,
                AddressId = a.AddressId,
                HouseNumber = a.HouseNumber,
                MunicipalCode = m.MunicipalName,
                ProvinceCode = p.ProvinceName,
                RegionCode = r.Region,
                StreetName = a.StreetName,
                UnitNumber = a.UnitNumber,
                VillageSubdivision = a.VillageSubdivision
            });

        return preAddress;
    }
}

这是我从客户端获取数据的方式:

服务

getAddress() {
    const endpoint = this.rootUrl + '/api/getaddress';
    const formData: FormData = new FormData();
    formData.append('personId', this.genParams.personId);
    return this.http.post(endpoint, formData);
}

组件

getPersonInformation() {
    this.clientService.getPerson(this.genParams.personId)
      .subscribe((data: any) => {
        console.log(data);
        this.person = data;
      });
}

在使用调试器的服务器之后,我实际上可以获得一个值,但在我的客户端。我收到以下错误: enter image description here

我需要你的帮助。谢谢。

2 个答案:

答案 0 :(得分:2)

尝试更新您的代码:

    [HttpPost]
    [Route("api/getaddress")]
    public PreAddress GetAddress()
    {
        var httpRequest = HttpContext.Current.Request;
        var id = httpRequest["personId"];
        int personId;
        if (!Int32.TryParse(id, out personId)) return null;

        PreAddress preAddress;

        using (var db = new ApplicationDbContext())
        {
            var preAddress = (from pa in db.PersonAndAddresses
                join a in db.Addresses on pa.AddressId equals a.AddressId
                join b in db.PersonBarangays on a.BarangayCode equals b.BarangayCode
                join m in db.PersonMunicipals on a.MunicipalCode equals m.MunicipalCode
                join p in db.PersonProvinces on a.ProvinceCode equals p.ProvinceCode
                join r in db.PersonRegions on a.RegionCode equals r.RegionCode
                where pa.PersonId == personId
                select new PreAddress()
                {
                    BarangayCode = b.BarangayName,
                    AddressId = a.AddressId,
                    HouseNumber = a.HouseNumber,
                    MunicipalCode = m.MunicipalName,
                    ProvinceCode = p.ProvinceName,
                    RegionCode = r.Region,
                    StreetName = a.StreetName,
                    UnitNumber = a.UnitNumber,
                    VillageSubdivision = a.VillageSubdivision
                });

            preAddress = preAddress.FirstOrDefault();
        }

       return preAddress; 
    }

答案 1 :(得分:1)

您需要在返回之前执行IQuerable,在此方案中尝试使用ToList()

[HttpPost]
[Route("api/getaddress")]
public IEnuerable<PreAddress> GetAddress()
{
    var httpRequest = HttpContext.Current.Request;
    var id = httpRequest["personId"];
    int personId;
    if (!Int32.TryParse(id, out personId)) return null;
    using (var db = new ApplicationDbContext())
    {
        var preAddress = (from pa in db.PersonAndAddresses
            join a in db.Addresses on pa.AddressId equals a.AddressId
            join b in db.PersonBarangays on a.BarangayCode equals b.BarangayCode
            join m in db.PersonMunicipals on a.MunicipalCode equals m.MunicipalCode
            join p in db.PersonProvinces on a.ProvinceCode equals p.ProvinceCode
            join r in db.PersonRegions on a.RegionCode equals r.RegionCode
            where pa.PersonId == personId
            select new PreAddress()
            {
                BarangayCode = b.BarangayName,
                AddressId = a.AddressId,
                HouseNumber = a.HouseNumber,
                MunicipalCode = m.MunicipalName,
                ProvinceCode = p.ProvinceName,
                RegionCode = r.Region,
                StreetName = a.StreetName,
                UnitNumber = a.UnitNumber,
                VillageSubdivision = a.VillageSubdivision
            });

        return preAddress.ToList(); //invoke it here
    }
}