Search code examples
c#odata

Is it possible to retrieve only some entities from OData API?


I am new to OData, and am wondering whether it is possible to retrieve only one entity from OData API? I have added the service to my VS project, and the automatically generated Reference.cs class contains all the entities that exist in the API. I only need one of the entities. Is it safe to simply delete the code referring to all the others? Or should I do something else?


Solution

  • The service reference is automatically generate and should be updated when the targeted service does. From your question is not clear why you want to delete the unnecessary entities but you can use the reference class to access the data you desire ignoring the rest.

    In your reference.cs class you should have a Container class. Initialize it var container = new Container(uri); If you need to authenticate you might want to do something like the below:

    container.Credentials = new NetworkCredential(userName, password);
    

    Then to access the MyEntity, you can use:

    var myEntities = container.MyEntities.ToList();
    

    but also

    container.MyEntities.Skip(100).Take(50)
    

    and so on.