ASP.NET MVC 5 Entity Join

asp.net-mvc c# entity-framework-6

Question

I'm new in ASP, Entity and lambda expressions. How can I join two tables?

Route Model:

public partial class Route
{
    public Route()
    {
        Flights = new HashSet<Flight>();
    }

    public int RouteID { get; set; }

    public int DepartureAirportID { get; set; }

    public int ArrivalAirportID { get; set; }

    public int FlightDuration { get; set; }

    public virtual Airport Airport { get; set; }

    public virtual Airport Airport1 { get; set; }

    public virtual ICollection<Flight> Flights { get; set; }
}

Airport Model:

public partial class Airport
{
    public Airport()
    {
        Routes = new HashSet<Route>();
        Routes1 = new HashSet<Route>();
    }

    public int AirportID { get; set; }

    public string City { get; set; }

    public string Code { get; set; }

    public virtual ICollection<Route> Routes { get; set; }

    public virtual ICollection<Route> Routes1 { get; set; }
}

SQL query looks like this:

SELECT a.AirportID, a.City
FROM Route r INNER JOIN Airport a ON r.ArrivalAirportID = a.AirportID
WHERE r.DepartureAirportID = @departureAirportID
ORDER BY a.City

Sorry for this easy question but I don't know how to do this with Entity Framework...

1
1
6/22/2015 9:59:42 AM

Popular Answer

Include with join entity framework. here doctorSendAnswerModel also a inner table.

 var data = _patientaskquestionRepository.Table.Include(x=>x.DoctorSendAnswer).Join(_patientRepository.Table, a => a.PatientId, d => d.Id, (a, d) => new { d = d, a = a }).Where(x => x.a.DoctorId == doctorid);
         if(!string.IsNullOrEmpty(status))
          data=data.Where(x=>x.a.Status==status);
          var result = data.Select(x => new {x= x.a,y=x.d }).ToList();
          var dt = result.Select(x => new PatientAskQuestionModel()
          {
              PatientId = x.x.PatientId.Value,
              AskQuestion = x.x.AskQuestion,
              Id = x.x.Id,
              DoctorId = x.x.DoctorId,
              FileAttachment1Url = x.x.FileAttachment1,
              DocName = x.y.FirstName + " " + x.y.LastName,
              CreatedDate = x.x.CreatedDate.Value,
              doctorSendAnswerModel = x.x.DoctorSendAnswer.Select(t => new DoctorSendAnswerModel { Answer = t.Answer }).ToList()
          }).ToList();


          return dt;
2
10/21/2015 5:49:08 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow