Skip to content

Instantly share code, notes, and snippets.

@cbertolasio
Last active December 14, 2015 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbertolasio/0f4911d7f2839d3c3d26 to your computer and use it in GitHub Desktop.
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
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>();
}
}
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;
}
{
"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"
}
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()
{
}
}
@cbertolasio
Copy link
Author

line #9 of ODataController.cs does a projection onto the POCO named WorkoutLogSummary and notice that an array of WorkoutLogSummary items are returned in the value[] of the json response...

@cbertolasio
Copy link
Author

RestSharpClient.cs shows how to execute the request to the odata endpoint using Rest Sharp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment