Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get request to api with predicate expression as a parameter

Is it possible to use a predicate expression as a parameter for an HTTPGET request (to a mvc api controller).

Background: I`m trying to build a car rental web application. using MSsql for db, EntityFramework (code-first), a repository for (generic) CRUD functions and MVC API controller.

My aim is to send queries to db through api controller using predicate linq expression. the code:

repository:

public IQueryable<T> Search<T>(Expression<Func<T, bool>> predicate) where T : class, IDataEntity
    {
        return _context.Set<T>().Where(predicate);
    }

api get(predicate Expression parameter) function:

public HttpResponseMessage Get(Expression<Func<Car, bool>> predicate)
    {
        using (IRepository Manager = Factories.Factory.GetRepository())
        {
            var CarList = Manager.Search<Car>(predicate)
                .Select(x => new CarDTO()
                {
                    ...
                })
                .ToList();
            return Request.CreateResponse<List<CarDTO>>(HttpStatusCode.OK, CarList);
        }
    }

client html angular/jq ajax request:

$http.get("/api/CarApi"+"??????",{params:{ ??????? }})
            .success(function (result) {
                $scope.filteredCarsList = result;
                ...
            })
            .error(function (result) {
                ...
            });

(Im using agulars $http for ajax request, similar to $.getJSON)

Is it possible to send a predicate expression through a ajax (get) request to an api controller who expect to get a linq expression as a parameter?

if so, how? and if not, what is the correct/appropriate way to achieve my goal?

like image 337
Ran Avatar asked May 16 '15 10:05

Ran


1 Answers

I'm not sure if this is useful to you, but you can use the Dynamic Linq Library. This way you can easily construct queries at runtime and query using strings.

More information found here.

like image 199
Luuk Moret Avatar answered Nov 01 '22 12:11

Luuk Moret