Created
March 24, 2014 09:13
-
-
Save yar-shukan/9736873 to your computer and use it in GitHub Desktop.
1-1 OData projection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Web.Http; | |
using System.Web.Http.OData.Query; | |
namespace ProjectionWebApi.Controllers | |
{ | |
[Route("api/projectioncheck")] | |
public class ProjectionCheckController : ApiController | |
{ | |
private static readonly IQueryable<User> UserSet = new[] | |
{ | |
new User | |
{ | |
Id = 1, | |
Name = "User 1", | |
Password = "jsfgshkdjghdfuretu", | |
SpendMoney = 200000, | |
Address = new Address {City = "Minsk", FullAddress = "Ylica 1-22", PostalCode = "220124"} | |
}, | |
new User | |
{ | |
Id = 2, | |
Name = "User 2", | |
Password = "ksjfhjkas238421 342", | |
SpendMoney = 2321300, | |
Address = new Address {City = "Boston", FullAddress = "Walk street 1-22", PostalCode = "98011"} | |
}, | |
new User | |
{ | |
Id = 3, | |
Name = "User 3", | |
Password = "asjfiwj riqrwer", | |
SpendMoney = 200000, | |
Address = new Address {City = "Brest", FullAddress = "Tolstogo 12-223", PostalCode = "340034"} | |
} | |
}.AsQueryable(); | |
[HttpGet] | |
[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)] | |
public IQueryable<User> Users() | |
{ | |
return UserSet; | |
} | |
} | |
public class User | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Password { get; set; } | |
public decimal SpendMoney { get; set; } | |
public Address Address { get; set; } | |
} | |
public class Address | |
{ | |
public string City { get; set; } | |
public string PostalCode { get; set; } | |
public string FullAddress { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://localhost:2853/api/projectioncheck?$select=Id,Address/PostalCode,Address/FullAddress&$expand=Address
Result JSON:
[{"Id":1,"Name":"User 1","Address":{"FullAddress":"Ylica 1-22","PostalCode":"220124"}},{"Id":2,"Name":"User 2","Address":{"FullAddress":"Walk street 1-22","PostalCode":"98011"}},{"Id":3,"Name":"User 3","Address":{"FullAddress":"Tolstogo 12-223","PostalCode":"340034"}}]