1

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.'

ganchito55
  • 3,350
  • 4
  • 27
  • 41
hncl
  • 2,331
  • 4
  • 58
  • 123

1 Answers1

1

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

JsonConvert.DeserializeObject<List<OhaMedication>>(stringData).AsQueryable()
ASpirin
  • 2,905
  • 1
  • 19
  • 27
  • Thank you, don't get the error however, I don't get include data. – hncl Oct 15 '17 at 17:17
  • of course, because you are loading all the data in your request execution and than returning it. You should configure your WebApi as OData service and use OData client. Check [this discussion](https://stackoverflow.com/q/39871420/514449) – ASpirin Oct 15 '17 at 18:15
  • Unfortunately I am not using OData and could not find any Microsoft tutorials. – hncl Oct 16 '17 at 16:37
  • The idea of `IQueryable` is not to store data but to provide the possibility to request the data. How would the `IQueryable` that was created from loaded data would get other data. Ofcourse you can create a custom `IQueryable` implementation for your service or use one that already existed or just load all required data and apply the filters before requests – ASpirin Oct 16 '17 at 18:35
  • Thank you. Since I am using Sqlite for offline storage, I used the Icollection to get the specific user data, then called the model from the local database as follows: var ohaMed = _theService.GetohaMedication().Where(x => x.Phn == phn).ToList(); foreach (var book in ohaMed) { ohaMed = _context.OhaMedication.Include(blog => blog.Frequency).Include(blog => blog.OhaType).ToList(); } BTW, oData is not officially supported in Core 2.0. – hncl Oct 17 '17 at 23:22