2015-06-22 38 views
1

我是新來的ASP,實體和lambda表達式。我怎樣才能加入兩張桌子?ASP.NET MVC 5實體加入

路線型號:

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; } 
} 

機場型號:

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查詢看起來是這樣的:

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 

對不起,這個簡單的問題,但我不知道該怎麼用Entity Framework做到這一點...

回答

1

LINQ查詢:

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 
1
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(); 
2

像這樣的東西應該做的(未經測試和您的查詢只是去上)具有可變硬編碼):

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; 
} 
1

你可以做到以下幾點:

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 
       }; 
0

看看this site,它會解釋你如何在Linq加入工作。 所以,如果你再次需要它,你將能夠自己解決它。

1

帶條件連接的實體查詢與分頁。

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(); 
2

包含與連接實體框架。這裏的doctorSendAnswerModel也是一張內在的表格。

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; 
1

你的實體和lembda查詢將被LOOL這樣的:

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();