brother
brother

Reputation: 8151

Custom property inside Web API Controller

I have something like the following in a WebAPI controller:

[EnableQuery()]
public IQueryable<ProductDTO> Get()
{
    var products = from p in db.Products
        select new ProductDTO()
        {
            Id = p.Id,
            Created = p.Created,
            Title = p.Title,
            CustomValue = ? (if Id=1 or 2, then CustomValue = 1, if Id=3, then CustomValue = 2 etc)
        };

    return products.AsQueryable();
}

I want to return a CustomValue based on the value of another property. In this example, the Id. So if the id is 1 or 2, CustomValue should return 1, if Id is 3, CustomValue should be 2 and so on. How can i do this?

Upvotes: 1

Views: 376

Answers (1)

levent
levent

Reputation: 3634

This is not about web api or controller,

public IQueryable<ProductDTO> Get()
{
    var products = from p in db.Products
        select new ProductDTO()
        {
            Id = p.Id,
            Created = p.Created,
            Title = p.Title,
            CustomValue = p.Id == 1 || p.Id == 2 ? 1 : p.Id == 3 ? 2 :0;
        };

    return products.AsQueryable();
}

Upvotes: 2

Related Questions