ASP.NET MVC 5 Entity Join

末鹿安然 提交于 2019-12-24 13:42:56

问题


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:


Something like this should do (untested and just going on from your query) with a variable hard-coded):

using (var db = new YourDbContext())
{
    var query = from r in db.Route
                join a in db.Airport a on r.ArrivalAirportID equals a.AirportID
                where r.DepartureAirportID = 1 // replace with your varialble.
                orderby a.City
                select a;
}



回答2:


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;



回答3:


LinQ query:

from r in context.Route
join  a in context.Airport 
on r.ArrivalAirportID equals a.AirportID
WHERE r.DepartureAirportID = "value"
ORDER BY a.City
select a.AirportID, a.City



回答4:


var balance = (from a in context.Airport 
               join c in context.Route on a.ArrivalAirportID equals c.AirportID
               where c.DepartureAirportID == @departureAirportID
               select a.AirportID)
              .SingleOrDefault();



回答5:


You can do the following:

var matches = from a in context.Airports
              join r in context.Routes 
                  on a.AirportID equals r.ArrivalAirportID
              where r.DepartureAirportID = departureAirportID
              order by a.City
              select new
              {
                  a.AirportID,
                  a.City
              };



回答6:


Entity query with conditional join with pagination.

  if (pageIndex <= 0)

            pageIndex = 1;

        pageIndex = ((pageIndex - 1) * pageSize) ;

        var patient = _patientRepository.Table.Join(_DoctorPatient.Table.Where(x => x.DoctorId == Id && x.IsBlocked==false), x => x.Id, d => d.PatientId, (x, d) => new { x = x });

        if (state != "")
            patient = patient.Where(x => x.x.State.Contains(state));
        if (name != "")
            patient = patient.Where(x => (x.x.FirstName + x.x.LastName).Contains(name));


        if (sdate != null)
            patient = patient.Where(x => x.x.CreatedDate >= sdate);
        if (eDate != null)
            patient = patient.Where(x => x.x.CreatedDate <= eDate);

        var result = patient.Select(x => x.x).Select(x => new PatientDoctorVM() { PatientId = x.Id, Id = x.Id, FirstName = x.FirstName, LastName = x.LastName, SSN = x.NewSSNNo, UserProfileId = x.UserProfileId, Email = x.Email, TumbImagePath = x.TumbImagePath }).OrderBy(x => x.Id).Skip(pageIndex).Take(pageSize).ToList();



回答7:


Your entity and lembda query will be lool like this:

  return (from d in _doctorRepository.Table 
       join p in _patientDoctor.Table on d.Id equals p.DoctorId 
      where p.PatientId == patientid.Value select d
       ).ToList();



回答8:


Take a look at this site, it will explain you how the join works in Linq. So if you ever need it again you will be able to solve it yourself.



来源:https://stackoverflow.com/questions/30977089/asp-net-mvc-5-entity-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!