I need to restrict the values that I return to my page in a web method without using some sort of proxy object.
Let’s say I have a Car class with the following class
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
public string Engine { get; set; }
}
and a webmethod which looks like;
[WebMethod]
public static List<Car> SearchCars(string search)
{
var cars = car.All().Where(x => x.name.StartsWith(search));
return cars.ToList();
}
The list that get’s return to my page has all attributes. How can I exclude say .. the engine attribute so only ID and Name are return?
The above is a purely fictional example, in the real world I’m using Subsonic3 objects to return a list etc.
You can use a projection to select only the fields you want returned. Something like this: