entity framework - Returning fields from multiple entities from ASP.NET Web API v2 -
i building asp.net web api v2 project using entity framework v6 model database. far have been able return collection of objects when collection of 1 object type. i.e. <customer>
or <order>
.
i return collection of objects include field (or property) related entity (an association present). think have linq entity query setup not sure how return resultant anonymous type jquery method calls api.
[route("api/clients/getallclientswithpaymentmethod")] [httpget] public not-sure-what-return-type-should-be getallclientswithpaymentmethod() { using (var context = new myentities()) { context.configuration.proxycreationenabled = false; var query = client in context.clients select new { clientid = client.id, companyname = client.companyname, phone = client.phone, email = client.email paymentmethod = client.payments.paymentmethod }; return query.tolist(); } }
is anonymous type proper way return information? if so, should return type be? there better way this?
i make class called client has fields return.
e.g.
public class client { private int id; private string companyname; private string phonenumber; private string email; private string paymethod; public client() { } //...etc
}
in controller create group of client objects results of database query , return ienumerable of clients e.g.
public ienumerable<client> getallclientswithpaymentmethod()
on client side can read content of message serializer using.
httpclient client = new httpclient(); httpresponsemessage response = await client.getasync("api/clients/getallclientswithpaymentmethod"); ienumerable<client> returnedclients = response.content.readasasync<client>(new[] { jsonformatter() }).result;
Comments
Post a Comment