entity framework - EF 6 and Web Api. How return custom data on GET? -


i need return web api controller specific data not exist in standard action. let's standard controller action is:

// api/xtourist/5         [responsetype(typeof(xtourist))]         public ihttpactionresult getxtourist(int id)         {             xtourist xtourist = db.xtourist.find(id);             if (xtourist == null)             {                 return notfound();             }              return ok(xtourist); 

but need return more data, let's hotel name. hotel name got using function:

public string findhotelname (int id)         {             int? hotelid = db.xtourist.find(id).КодИ;             string hotelname = db.xadres.find(hotelid).namec;             return hotelname;         } 

but how should joind these data , return in controller answer?

so want 2 results returned? why not create new object holds both values need?

public class touristreturndto {     public xtourist tourist { get; set; }     public string hotelname { get; set; } }  public ihttpactionresult getxtourist(int id) {     xtourist xtourist = db.xtourist.find(id);     if (xtourist == null)     {         return notfound();     }     string hotelname = findhotelname(id)     return ok(new touristreturndto         {             tourist = xtourist,             hotelname = hotelname         }     ); } 

don't need create touristreturndto, use anonymous type if want:

return ok(new          {             tourist = xtourist,             hotelname = hotelname         }     ); 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -