I've been exploring WEB API based on the guide at www.asp.net, and I've run into a problem when trying to filter columns from a table.
My code is simply this:
// GET api/Admin
public IEnumerable<Product> GetProducts(testvar = "")
{
IQueryable<Product> productString = db.Products;
if (testvar != "")
{
productString = productString.ToList().Select(i => new Product
{
Name = i.Name
}).AsQueryable();
}
return productString;
}
All I want to do is retrieve the Name column from the Product table for my JSON. However, instead of getting something like:
[{"Name":"Hammer"}] or [{"$id":"1","Name":"Hammer"}]
I'm getting:
[{"$id":"1","Id":0,"Name":"Hammer","Price":0.0,"ActualCost":0.0}]
The Price and ActualCost values are normally 16.99 and 20.00, but instead of removing them, they are just returned as 0. Same for Id.
Is there an easy way to stop it returning the Id, Price and ActualCost? Perhaps I'm missing something really simple, but the below two links are the closest I've come to finding something that might work.
The entity cannot be constructed in a LINQ to Entities query
As per the guide I've been using ( http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-entity-framework/using-web-api-with-entity-framework,-part-1 ) I'm working in an MVC 4 project with the Orders, Projects and OrderDetails tables.
Thanks