-
-
Save cbertolasio/0f4911d7f2839d3c3d26 to your computer and use it in GitHub Desktop.
shows how to do a projection via entity framework that will resolve to an odata uri
this is the GET that was issued from fiddler.... http://localhost:33203/odata/WorkoutLogs?$filter=UserName%20eq%20'8675309'&$orderby=DateCreated%20desc,WorkoutLogId%20desc&$inlinecount=allpages&$skip=5
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
public class WorkoutLogsController : EntitySetController<WorkoutLogSummary, int> | |
{ | |
private readonly DbContext context; | |
private DbSet<WorkoutLog> workoutLogs; | |
[Queryable(PageSize = 5)] | |
public override IQueryable<WorkoutLogSummary> Get() | |
{ | |
return workoutLogs.Select(it => new WorkoutLogSummary { | |
Notes = it.Note, | |
DateCreated = it.DateCreated, | |
UserName = it.User.IpNameIdentifier, | |
UserId = it.UserId, | |
WorkoutLogId = it.WorkoutLogId, | |
WorkoutName = it.Workout.Name, | |
WorkoutType = it.Workout.WorkoutType.Name | |
}); | |
} | |
protected override WorkoutLogSummary GetEntityByKey(int key) | |
{ | |
return workoutLogs.Where(it=> it.WorkoutLogId == key).Select(it => new WorkoutLogSummary { | |
Notes = it.Note, | |
DateCreated = it.DateCreated, | |
UserName = it.User.IpNameIdentifier, | |
UserId = it.UserId, | |
WorkoutLogId = it.WorkoutLogId, | |
WorkoutName = it.Workout.Name, | |
WorkoutType = it.Workout.WorkoutType.Name | |
}).FirstOrDefault(); | |
} | |
public WorkoutLogsController(DbContext context) | |
{ | |
this.context = context; | |
workoutLogs = this.context.Set<WorkoutLog>(); | |
} | |
} |
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
public string GetSummary() | |
{ | |
var client = new RestSharp.RestClient(HttpClientUtilities.GetBaseODataUri().ToString()); | |
var request = new RestSharp.RestRequest("WorkoutLogs", RestSharp.Method.GET); | |
request.RequestFormat = DataFormat.Json; | |
request.AddAuthorizationHeader(tokenProvider, scope); | |
request.AddParameter("$filter", string.Format("UserName eq '{0}'", claimsProvider.GetNameIdentifier())); | |
request.AddParameter("$orderby", "DateCreated desc,WorkoutLogId desc"); | |
request.AddParameter("$inlinecount", "allpages"); | |
request.JsonSerializer = new JsonSerializer(); | |
return client.Execute(request).Content; | |
} |
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
{ | |
"odata.metadata":"http://localhost:33203/odata/$metadata#WorkoutLogs", | |
"odata.count":"81", | |
"value":[ | |
{ | |
"DateCreated":"2013-03-02T21:45:00-07:00","UserId":3,"WorkoutLogId":"101","WorkoutName":"Bulger","UserName":"100000144938400","Notes":"test","WorkoutType":"The Heros" | |
},{ | |
"DateCreated":"2013-03-02T21:45:00-07:00","UserId":3,"WorkoutLogId":"100","WorkoutName":"Bulger","UserName":"100000144938400","Notes":"test","WorkoutType":"The Heros" | |
},{ | |
"DateCreated":"2013-03-02T21:00:00-07:00","UserId":3,"WorkoutLogId":"108","WorkoutName":"Bulger","UserName":"100000144938400","Notes":"test","WorkoutType":"The Heros" | |
},{ | |
"DateCreated":"2013-03-02T01:30:00-07:00","UserId":3,"WorkoutLogId":"97","WorkoutName":"Bulger","UserName":"100000144938400","Notes":null,"WorkoutType":"The Heros" | |
},{ | |
"DateCreated":"2013-03-02T00:00:00-07:00","UserId":3,"WorkoutLogId":"95","WorkoutName":"Bulger","UserName":"100000144938400","Notes":null,"WorkoutType":"The Heros" | |
} | |
], | |
"odata.nextLink":"http://localhost:33203/odata/WorkoutLogs?$filter=UserId%20eq%203&$orderby=DateCreated%20desc%2CWorkoutLogId%20desc&$inlinecount=allpages&$skip=10" | |
} |
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
public class WorkoutLogSummary | |
{ | |
public Nullable<DateTimeOffset> DateCreated { get; set; } | |
public int UserId { get; set; } | |
public long WorkoutLogId { get; set; } | |
public string WorkoutName { get; set; } | |
public string UserName { get; set; } | |
public string Notes { get; set; } | |
public string WorkoutType { get; set; } | |
public WorkoutLogSummary() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RestSharpClient.cs shows how to execute the request to the odata endpoint using Rest Sharp