.net Core 2.0 Web API - Newtonsoft.Json.JsonSerializationException - IQueryable

asp.net-core-webapi c# entity-framework-core json

Question

I have two separate .net core applications, Web API and client. I get the model using:

public IEnumerable<OhaMedication> GetOhaMedication()
{
    return _context.OhaMedication;
}

The model:

public class OhaMedication
{
    public int OhaMedicationId { get; set; }
    public string Phn { get; set; }
    public int OhaTypeId { get; set; }
    public int FrequencyId { get; set; }

    public MedFrequancy Frequency { get; set; }
    public OhaType OhaType { get; set; }
}

public class OhaType
{
    public OhaType()
    {
        OhaMedication = new HashSet<OhaMedication>();
    }

    public int OhaTypeId { get; set; }
    public string Name { get; set; }

    public ICollection<OhaMedication> OhaMedication { get; set; }
}


public class MedFrequancy
{
    public MedFrequancy()
    {
        OhaMedication = new HashSet<OhaMedication>();
    }

    public int FrequencyId { get; set; }
    public string Frequency { get; set; }

    public ICollection<OhaMedication> OhaMedication { get; set; }
}

In the client I use the following to get the data:

public IQueryable<OhaMedication> GetohaMedication()
{
    var dir = _session.GetString(SessionsKeys.Directory);
    bool connection = InternetConnection.Check(_webApiData.WebApiitems.Url);
    if (connection)
    {
        using (HttpClient client = new HttpClient())
        {
            client.BaseAddress = new Uri(_webApiData.WebApiitems.Url);
            MediaTypeWithQualityHeaderValue contentType =
                new MediaTypeWithQualityHeaderValue("application/json");
            client.DefaultRequestHeaders.Accept.Add(contentType);
            HttpResponseMessage response = client.GetAsync("/OhaMedications").Result;
            string stringData = response.Content.ReadAsStringAsync().Result;
            IQueryable<OhaMedication> data = JsonConvert.DeserializeObject<IQueryable<OhaMedication>>(stringData);
            return data;
        }
    }
    else
        return _context.OhaMedication;
}

I need to use IQueryable to use include as follows:

var ohaMed = GetohaMedication().Where(x => x.Phn == phn).Include(o => o.Frequency)
.Include(o => o.OhaType).ToList();

I get the following error:

Newtonsoft.Json.JsonSerializationException: 'Cannot create and populate list type System.Linq.IQueryable`1[WebUI.Data.DataModels.OhaMedication]. Path '', line 1, position 1.'

1
1
10/15/2017 8:49:40 AM

Accepted Answer

It cannot create an interface because doesnot know which type it actually should use. Try:

JsonConvert.DeserializeObject<List<OhaMedication>>(stringData).AsQueryable()
1
10/15/2017 6:10:20 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow