7
votes

I am looking for a sample Odata application by using WebApi, but without using EF. My requirement is to implement a couple of action methods inside a controller (derived from ODataController) that return complex objects that can be queried. These action methods should also be able to take multiple parameters. The data returned is being pulled from multiple data sources, so using entity framework is not an option. I would like to implement a method like this:

public List RetrieveCustomersByFilter(string name, string lastName, CustomerTypeEnum) { ...business logic goes here ... }

I have done a lot of research online, but I am still not able to find a concrete example. Most of them show simple methods that don't accept any parameters (or an id/key) and return a List of standard objects.

Can anyone provide a sample or point me to a link that shows how to go about this?

Thanks

2
Check the tour to learn what is and isn't on topic here, kthxuser1228
@AgentHut, I add some sample codes at here. Hope it can help you. And you can refer to Web API OData repository for more function related test cases. Thanks.Sam Xu

2 Answers

0
votes

check this out has the latest Web API 2 with ODATA sample with all steps ODATA v4 sample with asp.net web api 2

-2
votes

I guess you need to use agile development. create first a model class.

public class Model{
    public string ID {get; set;}
    public string name {get; set;}
}

then create an dal class,

public class Dal{
     private Model model = new Model();
     public List<Model>getAllRecords()
    {
       List<model> list = new List<model>();
       .....
       .....
       list.Add(new Model{
            ID =.....,
            name = ...
        });
    }
}

then create a logic class that will get the data from dal, and use linq for querying the data and you can also filter the data you want to get inside

  public class Logic{
        private DAL dal = new DAL();

        public dynamic dataGet(int id, string name){
         var a = (from data where dal.getAllRecords()
                   where data.ID == id && data.name == name
                   select new{
                        dataid = data.ID
                        daname = data.name   
                   }).ToList();
          return a;
       }
 }

I hope it will helps you